最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - clearTimeout not working in javascript autocomplete script - Stack Overflow

programmeradmin0浏览0评论

I am using the following code as part of an autoplete 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?

Thanks!

I am using the following code as part of an autoplete 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?

Thanks!

Share Improve this question asked Apr 11, 2010 at 22:00 TravisTravis 2,0214 gold badges22 silver badges31 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 10

You probably want to use:

that.timer = setTimeout (that.doStuff, 2000);

instead of:

that.timer = setTimeout (that.doStuff(), 2000);

Otherwise, doStuff will be called immediately.

发布评论

评论列表(0)

  1. 暂无评论