Kcko
7/24/2017 - 7:23 PM

jQuery - keyup with delay (timeout)

jQuery - keyup with delay (timeout)

var delay = (function(){
  var timer = 0;
  return function(callback, ms){
    clearTimeout (timer);
    timer = setTimeout(callback, ms);
  };
})();


$('input').keyup(function() {
    delay(function(){
      alert('Time elapsed!');
    }, 1000 );
});
var timeout = null
$('input').on('keyup', function() {
    var text = this.value
    clearTimeout(timeout)
    timeout = setTimeout(function() {
        // Do AJAX shit here            
        console.log(text)
    }, 500)
})
$('#mySearch').keyup(function() {

  var $this = $(this);

  clearTimeout($.data(this, 'timer'));
  var wait = setTimeout(function(){


          $.get("query.php?q="+$this.val());


  }, 1);
  $(this).data('timer', wait);
});