You can use what I call a one-shot timer for this. The name comes from a
one-shot timer in electronic circuits - it may not be exactly the right
name, but it does what you want. :-)

First, you need a function that creates a one-shot timer:

    function oneshot() {
        var timer;
        return function( fun, time ) {
            clearTimeout( timer );
            timer = setTimeout( fun, time );
        };
    }

Then, use that function to create a specific one-shot timer for your
autosuggest:

    var suggestOneshot = oneshot();

Finally, use that one-shot timer. Let's say you have this function call in
your keyup handler:

    doAutoSuggest();

Assuming you want to wait 1/2 second after the user stops typing, replace
that function call with:

    suggestOneshot( doAutoSuggest, 500 );

That will call doAutoSuggest once, 500ms after the end of any burst of
typing with less than 500ms between keystrokes.

-Mike

> From: chris at zeus
> 
> I've got an autosuggest script that runs every onkeyup.  So 
> for every key stroke, the script calls the server.
> 
> I would much rather see this script call the server after the 
> user stops typing, or if while typing, after a set number of 
> microseceonds have passed before calling the server to run 
> its search suggestions.
> 
> Any thoughts?

Reply via email to