Skip to content

Instantly share code, notes, and snippets.

@Kcko
Last active October 3, 2023 11:36
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save Kcko/0ad5d77c5f9f8beb6d1809ddd91d9840 to your computer and use it in GitHub Desktop.
Save Kcko/0ad5d77c5f9f8beb6d1809ddd91d9840 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