jQuery添加和删除延迟 [英] jQuery add and remove delay
问题描述
如何在jQuery中得到这个:在延迟(3s)之后调用一些带有工作代码的函数,如果我在第一次调用延迟之前再次调用函数,则重置延迟并调用新的3s。
how a can get this, in jQuery: call some function with working code after delay(3s), IF i call function again before delay from first call in not done, then reset delay and call new 3s.
示例:
fce MeDeday(3s) - 时间过后 - 警告(你好);
fce MeDeday(3s) - after time down - alert("hello");
情况1:
调用MeDelay() - 时间下降 - 警告(你好)
call MeDelay() - time down - alert("hello")
情况2:
致电MeDelay()
call MeDelay()
从第一次通话开始仍然是2s
still remain 2s from first call
重置时间并再次等待3s,没有2s + 5s并且被解雇2次警报(你好)
reset time and again wait 3s, no 2s + 5s and fired 2times alert("hello")
调用MeDelay() - 时间下降 - 警告(你好)
call MeDelay() - time down - alert("hello")
推荐答案
这称为 debouncing,,与限制密切相关。有一个很好的jQuery插件: jQuery油门/去抖动。
This is called debouncing, which is closely related to throttling. There's a nice jQuery plugin for that: jQuery throttle/debounce.
我认为这是你正在寻找的用例:
I think this is the use case you're looking for:
function fn()
{
alert('hello');
}
var debouncedFn = $.debounce(3000, fn);
$('#my-button').click(debouncedFn);
这篇关于jQuery添加和删除延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!