Ways to throttle ajax requests
Asked Answered
P

1

8

I'm using the following code (written by another user) to throttle ajax requests in a livesearch function:

JSFiddle if you prefer a demo: http://jsfiddle.net/4xLVp/

It seems buggy, though. Clearing values with Ctrl+shift+back-arrow, and typing again causes a flurry of requests. Blank values also cause a request. It dones't seem right, especially compared to jQuery UI autocomplete, where request delays seem more measured.

    $('##tag-search').keyup(function() {
        var elem = $(this);
        if (elem.val().length >= 2) {
            elem.data('search',search).clearQueue().stop().delay(1000).queue(function() {
                $.ajax({ // ajax stuff
                    'success': function(data){ /*show result*/ }
                });
                if (elem.data('search') !=  string) return;
            });                                             
        } else if (string.length <= 1) { /*show original content*/ }
    });

Is there a better way to handle this?

Paronychia answered 6/8, 2011 at 15:23 Comment(0)
D
9

I would just use setTimeout:

(function() {
    var timeout;
    $('#tag-search').keyup( function() {
        var elem = $(this);
        if (elem.val().length >= 2) {
            clearTimeout(timeout);
            timeout = setTimeout(function() {
               $.ajax({ // ajax stuff
                    'success': function(data){ /*show result*/ }
                });     
            }, 80); // <-- choose some sensible value here                                      
        } else if (string.length <= 1) { /*show original content*/ }
    });
}());

There is also a debounce/throttle plugin.

Desrochers answered 6/8, 2011 at 15:43 Comment(4)
Thanks. That's done the trick, I'm surprised it's that simple. Can you please explain what the clearTimeout() does and why it's needed?Paronychia
setTimeout - sets a code which would be executed after some time, clearTimeout - removes that pending execution, it's needed to clear earlier peding ajax request before starting new oneInvocation
BTW - I do not recommend removal of if (elem.data('search') != string) return; part, it prevents earlier ajax calls (lasting longer) from updating already changed resultsInvocation
@Jacek_FH: How? As far as I can see, this statement is doing nothing. It is not inside the Ajax call. The function will return after that statement anyway. Edit: Ah, in the original code (the one that the OP links to) this statement is inside the Ajax callback.Desrochers

© 2022 - 2024 — McMap. All rights reserved.