JQuery + Keyup + Ajax

Here is a nice way to use "keyup" for ajax requests with a delay. That allows user to type in the whole word and using delay we can limit the number of server requests.
var interval = null;
$('input#input_id').on('keyup', function() {
    var text = this.value;
    clearTimeout(interval);
    interval = setTimeout(function() {
      if(text){  
        $.ajax({
          url: "/page/",
          data: { q: text.trim() },
          dataType: "script"
        });
      }
    }, 600)
});

Comments

Popular posts from this blog

Interview Question on Ruby on Rails

Inserting and Moving elements inside Ruby Array