如何重用此 JavaScript 超时关闭?
我发现了以下一段 JavaScript 代码(也许在 Stack Overflow 上?)用于实现超时:
var delay = (function() {
var timer = 0;
return function(callback, ms) {
clearTimeout(timer);
timer = setTimeout(callback, ms);
};
})();
我是 JavaScript 新手,所以我仍在尝试解决闭包问题。如果我在一个地方调用 delay(firstCallback, 200)
,然后立即调用 delay(secondCallback, 200)
,第一个超时回调将被清除,而第二个回调将成功执行。
如何在不同实例中重用delay
而不覆盖其他实例? (不确定这些术语是否正确,但希望它能表达我的观点。)因此,在上面的示例中,我希望两个回调都执行。
感谢您的帮助!
编辑:作为一个实际的例子,我试图在输入字段上缓冲按键事件,以便回调仅在 200 毫秒内没有按下任何按键后执行。但我有多个输入字段,当前当两个输入字段快速连续发生按键事件时缓冲区会中断。
I found the following piece of JavaScript code (maybe here at Stack Overflow?) for implementing a timeout:
var delay = (function() {
var timer = 0;
return function(callback, ms) {
clearTimeout(timer);
timer = setTimeout(callback, ms);
};
})();
I'm new to JavaScript, so I'm still trying to wrap my head around closures. If I call delay(firstCallback, 200)
in one place and then delay(secondCallback, 200)
right after, the first timeout callback is cleared, while the second callback executes successfully.
How do I reuse delay
in different instances without overwriting other instances? (Not sure if those terms are correct, but hopefully it gets my point across.) So, in the above example, I want both callbacks to execute.
Thanks for your help!
EDIT: As a practical example, I'm trying to buffer keypress events on an input field, so that the callback is only executed after no key has been pressed for 200ms. But I have multiple input fields, and currently the buffer breaks when two input fields have keypress events in quick succession.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
要重用它,更容易摆脱匿名函数并将其用作生成器。
这是一个工作小提琴:
http://jsfiddle.net/HJrM7/
但是,值得注意的是,这个关闭的目的是以防止多个回调针对某个对象堆积。这是一个非常好的闭包,它使您能够确保最后触发的事件将是被执行的事件。我经常使用这种技术来防止闪烁或在重绘期间不需要的鼠标移出事件。
To reuse this, it is easier to get rid of the anonymous function and use it as a generator.
Here is a working fiddle:
http://jsfiddle.net/HJrM7/
But, It is worth noting, that the point of this closure is to prevent multiple callbacks from getting stacked against some object. It is a really nice closure though that enables you to make sure that the last event triggered will be the one that gets acted on. I use this technique a lot to prevent flickering, or unwanted mouse out events during ie redraws.
您正在寻找
jsfiddle 上的示例: http://jsfiddle.net/TF9Tw/1/
You are looking for
Example on jsfiddle: http://jsfiddle.net/TF9Tw/1/
闭包保存在内部函数调用之间持续存在的状态。在本例中使用它,因此一次只有一次超时。如果您希望一次有多个超时可用,您可以只编写
编辑:
对于您的示例,最好的解决方案是制作一个像这样的对象
,而不是 someDelayManager 您将使用每个输入元素唯一的某个对象的成员变量。如需增加延迟,您可以致电。
The closure holds state that persists between calls of the inner function. In this case it is used so there is only one timeout at a time. If you want multiple timeouts available at a time you may just write
edit:
For your example, the best solution would be to make an object like this
Where instead of someDelayManager you would use a member variable of some object unique per input element. To add a delay you would then call.