Skip to content

Instantly share code, notes, and snippets.

@dawnerd
Created April 9, 2012 21:13
Show Gist options
  • Save dawnerd/2346585 to your computer and use it in GitHub Desktop.
Save dawnerd/2346585 to your computer and use it in GitHub Desktop.
Debounce JS programming challenge
Debounce
Debouncing is a way of ensuring that a function is only called once when fired multiple times in a short period. Almost all keyboards have some form of built in debouncing since the keys will hit the sensor quite a few times per keystroke. For keyboards, the delay is somewhere around 50ms.
In Javascript, debouncing is important for rapidly firing events, such as text input. Instead of firing a keydown event and processing input data on every keypress, you fire the event once after no more keypresses are detected in a certain amount of time.
--
Scoring:
1 point for correctly working solution
1 point for bonus feature
The challenge:
Write a function that will debounce input from the user. The keydown callback function should only be fired once per delay period (250ms). For example, you should bind to the keyup event and output the debounced input to the div UserOutput. Output should be on a new line.
Bonus:
Convert your function into a prototype.
Below is sample code to get you started:
<html>
<body onload="onLoad()">
<script>
function onLoad(){
var UserInput = document.getElementById('UserInput');
var UserOutput = document.getElementById('UserOutput');
//YOUR CODE HERE
//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