Skip to content

Instantly share code, notes, and snippets.

@wisnubaldas
Forked from Kcko/keyup-delay1.js
Created August 19, 2022 02:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wisnubaldas/f5e89211bb4dacdf0331655a91f10496 to your computer and use it in GitHub Desktop.
Save wisnubaldas/f5e89211bb4dacdf0331655a91f10496 to your computer and use it in GitHub Desktop.
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);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment