Last active
December 21, 2017 06:05
-
-
Save encoreshao/0782fbdf54f091d76bed1e686f21dab3 to your computer and use it in GitHub Desktop.
Keyup Function with Delay
This file contains 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
// 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