-
-
Save wisnubaldas/f5e89211bb4dacdf0331655a91f10496 to your computer and use it in GitHub Desktop.
jQuery - keyup with delay (timeout)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 ); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var timeout = null | |
$('input').on('keyup', function() { | |
var text = this.value | |
clearTimeout(timeout) | |
timeout = setTimeout(function() { | |
// Do AJAX shit here | |
console.log(text) | |
}, 500) | |
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('#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