Skip to content

Instantly share code, notes, and snippets.

@encoreshao
Last active December 21, 2017 06:05
Show Gist options
  • Save encoreshao/0782fbdf54f091d76bed1e686f21dab3 to your computer and use it in GitHub Desktop.
Save encoreshao/0782fbdf54f091d76bed1e686f21dab3 to your computer and use it in GitHub Desktop.
Keyup Function with Delay
// in Javascript file
var realtimeSearchWithDelay = (function(){
var timer = 0;
return function(callback, ms){
clearTimeout (timer);
timer = setTimeout(callback, ms);
};
})();
$('input.realtime-search').keyup(function() {
realtimeSearchWithDelay(function(){
alert('Time elapsed!');
}, 1000 );
});
// in Coffee file
window.realtimeSearchWithDelay = do ->
timer = 0
(callback, ms) ->
clearTimeout timer
timer = setTimeout(callback, ms)
return
return
$(document).on 'keyup', 'input.realtime-search', (e) ->
e.preventDefault()
e.stopPropagation()
realtimeSearchWithDelay (->
alert('Time elapsed!');
), delayTime
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment