Jquery随着延迟而改变 [英] Jquery change with delay
问题描述
可能重复:
在jQuery中调用事件调用
我喜欢.change ()jQuery的功能,但我想在用户快速更改选择下拉列表中的选项时阻止触发大量的AJAX请求。例如,当用户使用鼠标滚轮时,它会在选择新选项时触发每个选项。
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?
推荐答案
执行此操作的典型方法是使用setTimeout和clearTimeout:
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);
});
这篇关于Jquery随着延迟而改变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!