Created
April 10, 2012 00:07
-
-
Save dawnerd/2347518 to your computer and use it in GitHub Desktop.
debounce solution
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
<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