[jQuery] Delay on keypress

[jQuery] Delay on keypress

Thanks Erik that did the trick!
- David Dexter
-----Original Message-----
From: discuss-bounces@jquery.com [mailto:discuss-bounces@jquery.com] On
Behalf Of Erik Beeson
Sent: Thursday, March 15, 2007 8:59 PM
To: jQuery Discussion.
Subject: Re: [jQuery] Delay on keypress
You basically want to have a timer that gets "canceled" if a key is
pressed before it executes. Something like this:
var search_timeout = undefined;
$(...).bind('keyup', function() {
    if(search_timeout != undefined) {
        clearTimeout(search_timeout);
    }
    var $this = this; // save reference to 'this' so we can use it in
timeout function
    search_timeout = setTimeout(function() {
        search_timeout = undefined;
        // do stuff with $this here
    }, 500);
});
The 500 is the delay in ms. All that undefined checking might not be
necessary, I'm just a paranoid programmer :) Good luck.
--Erik























    • Topic Participants

    • david