Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
40
rated 0 times [  44] [ 4]  / answers: 1 / hits: 35188  / 13 Years ago, fri, february 24, 2012, 12:00:00

Possible Duplicate:

Throttle event calls in jQuery






I like the .change() functionality of jQuery, but I'd like to prevent triggering a ton of AJAX requests when a user quickly changes options in a select drop down. As an example, when a user uses a mouse scroll wheel, it will trigger each options as they pick their new option.



I'd like to come up with a good clean way to handle only sending these updates once the user stops updating the select dropdown a second.



Is there a neat way of handling this situation?


More From » jquery

 Answers
0

The typical way to do this is with a setTimeout and clearTimeout:



var wto;

$('#select').change(function() {
clearTimeout(wto);
wto = setTimeout(function() {
// do stuff when user has been idle for 1 second
}, 1000);
});

[#87243] Thursday, February 23, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tyasiaalmap

Total Points: 294
Total Questions: 107
Total Answers: 108

Location: Libya
Member since Mon, Dec 7, 2020
4 Years ago
;