Thanks Erik that did the trick!

- David Dexter


-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 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

On 3/15/07, blemming <[EMAIL PROTECTED]> wrote:
>
> I'm trying to put together a live search input for an internal app I'm
> working on and I can't seem to figure out how to delay the ajax call to
only
> fire if the user hasn't typed a character for half a second or so.  That
> way, the request isn't firing after every character is entered as the user
> is typing.
>
> Here is what I'm using, but I would like to build in some functionality
> $('#search').bind('keyup',function() {
> str = $('#search').val();
>         if(str.length >= 3){
>                 $.ajax(.....)
>         }
> }
>
> Thanks for the help.....
>
> Cheers, David
> --
> View this message in context:
http://www.nabble.com/Delay-on-keypress-tf3412467.html#a9508255
> Sent from the JQuery mailing list archive at Nabble.com.
>
>
> _______________________________________________
> jQuery mailing list
> discuss@jquery.com
> http://jquery.com/discuss/
>

_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/



_______________________________________________
jQuery mailing list
discuss@jquery.com
http://jquery.com/discuss/

Reply via email to