clearTimeout无法在javascript自动完成脚本中使用 [英] clearTimeout not working in javascript autocomplete script
本文介绍了clearTimeout无法在javascript自动完成脚本中使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下代码作为自动完成脚本的一部分,以避免每次按键敲击服务器:
I am using the following code as part of an autocomplete script to avoid hammering the server with every keystroke:
var that = this;
textInput.bind("keyup", function() {
clearTimeout(that.timer);
that.timer = setTimeout (that.doStuff(), 2000);
});
不幸的是,这并没有清除旧的计时器。他们仍然都执行。
Unfortunately, this does not clear the old timers. They still all execute.
有谁知道我错过了什么?
Does anyone know what I'm missing?
谢谢!
推荐答案
您可能想要使用:
that.timer = setTimeout (that.doStuff, 2000);
而不是:
that.timer = setTimeout (that.doStuff(), 2000);
否则,将立即调用doStuff。
Otherwise, doStuff will be called immediately.
这篇关于clearTimeout无法在javascript自动完成脚本中使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文