admin管理员组

文章数量:1391937

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.

本文标签: jqueryclearTimeout not working in javascript autocomplete scriptStack Overflow