Crazywako
Crazywako

Reputation: 362

Perform AJAX search one second after multiple keydown events

So I am making a autocomplete with jquery. Ajax is used to get a heavy xml file from remote location. If I search for ex. "Two and a half men" it does about 16 querys (for every letter typed except the 3 first, look the code) with ajax.

I want it to do the Ajax request a second after user has pressed the last key so it will do one query instead of 16.

So if user presses another key before it's been 1 second since the last keydown event, it won't do the query yet.

<script>
    $(document).ready(function(){
        $('#tv_search').keydown(function(){
            var search = $(this).val();
            if(search.length >= 3)
            {
                $.ajax({
                  type: "GET",
                  url: "search.php",
                  data: {show : search, key : '890k4900k0ll' }
                }).done(function(msg){
                    $('#t').html(msg);
                });
            }
        });
    });
</script>

Search for series: <input type='text' name='tv_search' id='tv_search'>

Any ideas?

Upvotes: 4

Views: 5358

Answers (4)

adeneo
adeneo

Reputation: 318212

You could just use a timeout, and clear it every time a key is pressed :

$(document).ready(function() {
    $('#tv_search').on('keyup', function() {
        clearTimeout($(this).data('timer'));
        var search = this.value;
        if (search.length >= 3) {
            $(this).data('timer', setTimeout(function() {
                $.ajax({
                    type: "GET",
                    url: "search.php",
                    data: {
                        show: search,
                        key: '890k4900k0ll'
                    }
                }).done(function(msg) {
                    $('#t').html(msg);
                });
            }, 1000));
        }
    });
});​

Upvotes: 7

sssurii
sssurii

Reputation: 830

quit simple solution :

<script>
$(document).ready(function(){
var mytimer;
    $('#tv_search').keydown(function(){
       clearTimeout(mytimer);
        var search = $(this).val();
        if(search.length >= 3)
        {
           mytimer = setTimeout(function(){
             $.ajax({
              type: "GET",
              url: "search.php",
              data: {show : search, key : '890k4900k0ll' }
            }).done(function(msg){
                $('#t').html(msg);
            });
         }, 1000);
        }
    });
});

Where 'mytimer' must be global variable, simply clear previous 'mytimer' on every event call. It will only execute latest call in interval of 1 second.

Upvotes: 0

Miguel Q
Miguel Q

Reputation: 3618

I have a solution that allows you to delay any method you call, avoiding repetitive calls, that would be not used at all!

https://stackoverflow.com/a/30503848/1834212

Upvotes: 0

Talha Ahmed Khan
Talha Ahmed Khan

Reputation: 15433

To avoid multiple AJAX request generating at each key-press try to use setTimeout() and clearTimeout() methods in a way that you cancel the last timeout and start a new timeout by calling setTimeout() after clearing it. setTimeout() method should contain the AJAX request which executes after quarter of second (250 millis) user press a key.

I also abort() the request if its available.

var timeOut = null,
    myXHR = null;
$(document).ready(function(){
    $('#tv_search').keydown(function(){
        // It will clear the setTimeOut activity if valid.
        if(timeOut) clearTimeout(timeOut);

        var search = $(this).val();
        if(search.length >= 3)
        {
            timeOut = setTimeout(function(){
              // Cancel the last request if valid
              if(myXHR) myXHR.abort();

              myXHR = $.ajax({
                type: "GET",
                url: "search.php",
                data: {show : search, key : '890k4900k0ll' }
              }).done(function(msg){
                  $('#t').html(msg);
              });
            }, 250);// wait for quarter second.
        }
    });
});

Upvotes: 3

Related Questions