Skip to content

Instantly share code, notes, and snippets.

@dawnerd
Created April 10, 2012 00:07
Show Gist options
  • Save dawnerd/2347518 to your computer and use it in GitHub Desktop.
Save dawnerd/2347518 to your computer and use it in GitHub Desktop.
debounce solution
<html>
<body onload="onLoad()">
<script>
function onLoad(){
var UserInput = document.getElementById('UserInput');
var UserOutput = document.getElementById('UserOutput');
//YOUR CODE HERE
Function.prototype.debounce = function(delay_period) {
var func = this,
timer;
return function delayed() {
var self = this,
arg = arguments;
timer && clearTimeout(timer);
timer = setTimeout(function(){
func.apply(self, arg);
clearTimeout(timer);
}, delay_period || 100);
}
}
UserInput.onkeyup = function(event){
if(event.target.value.length) UserOutput.innerHTML += event.target.value+'<br>';
}.debounce(250);
//END YOUR CODE
}
</script>
<input id="UserInput">
<div id="UserOutput"></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment