如何重用此 JavaScript 超时关闭?

发布于 2024-10-25 19:18:54 字数 611 浏览 3 评论 0原文

我发现了以下一段 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

柒七 2024-11-01 19:18:54

要重用它,更容易摆脱匿名函数并将其用作生成器。

var createDelayManager = function() {
  var timer = 0;
  return function(callback, ms) {
     clearTimeout(timer);
     timer = setTimeout(callback, ms);
  };
}


var delayManagerOne = createDelayManager();
delayManagerOne(firstCallback, 200);

var delayManagerTwo = createDelayManager();
delayManagerTwo(secondCallback, 200);

这是一个工作小提琴:
http://jsfiddle.net/HJrM7/

但是,值得注意的是,这个关闭的目的是以防止多个回调针对某个对象堆积。这是一个非常好的闭包,它使您能够确保最后触发的事件将是被执行的事件。我经常使用这种技术来防止闪烁或在重绘期间不需要的鼠标移出事件。

To reuse this, it is easier to get rid of the anonymous function and use it as a generator.

var createDelayManager = function() {
  var timer = 0;
  return function(callback, ms) {
     clearTimeout(timer);
     timer = setTimeout(callback, ms);
  };
}


var delayManagerOne = createDelayManager();
delayManagerOne(firstCallback, 200);

var delayManagerTwo = createDelayManager();
delayManagerTwo(secondCallback, 200);

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.

小鸟爱天空丶 2024-11-01 19:18:54

您正在寻找

Delay = function(callback, ms) {
    this.callback = callback;
    this.ms = ms;
};

Delay.prototype = {
    timer: -1,

    restart: function() {
        if (this.timer != -1) clearTimeout(this.timer);
        this.timer = setTimeout(this.callback, this.ms);
    }
};

var delay1 = new Delay(callback1, 100);
var delay2 = new Delay(callback2, 100);

// on some event1
delay1.restart();

// on some event2
delay2.restart();

jsfiddle 上的示例: http://jsfiddle.net/TF9Tw/1/

You are looking for

Delay = function(callback, ms) {
    this.callback = callback;
    this.ms = ms;
};

Delay.prototype = {
    timer: -1,

    restart: function() {
        if (this.timer != -1) clearTimeout(this.timer);
        this.timer = setTimeout(this.callback, this.ms);
    }
};

var delay1 = new Delay(callback1, 100);
var delay2 = new Delay(callback2, 100);

// on some event1
delay1.restart();

// on some event2
delay2.restart();

Example on jsfiddle: http://jsfiddle.net/TF9Tw/1/

醉南桥 2024-11-01 19:18:54

闭包保存在内部函数调用之间持续存在的状态。在本例中使用它,因此一次只有一次超时。如果您希望一次有多个超时可用,您可以只编写

setTimeout(callback, ms);

编辑:

对于您的示例,最好的解决方案是制作一个像这样的对象

function DelayManager(){}
DelayManager.prototype.timer = 0;
DelayManager.prototype.delay = function(callback, ms) {
  clearTimeout(this.timer);             
  this.timer = setTimeout(callback, ms);   
}
someDelayManager = new DelayManager();

,而不是 someDelayManager 您将使用每个输入元素唯一的某个对象的成员变量。如需增加延迟,您可以致电。

someDelayManager.delay(callback, ms);

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

setTimeout(callback, ms);

edit:

For your example, the best solution would be to make an object like this

function DelayManager(){}
DelayManager.prototype.timer = 0;
DelayManager.prototype.delay = function(callback, ms) {
  clearTimeout(this.timer);             
  this.timer = setTimeout(callback, ms);   
}
someDelayManager = new DelayManager();

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.

someDelayManager.delay(callback, ms);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文