Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 960
Crazywako Avatar asked Sep 16 '25 00:09

Crazywako


2 Answers

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));
        }
    });
});​
like image 68
adeneo Avatar answered Sep 18 '25 16:09

adeneo


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.
        }
    });
});
like image 27
Talha Ahmed Khan Avatar answered Sep 18 '25 18:09

Talha Ahmed Khan