Created
January 30, 2012 21:34
-
-
Save mattpodwysocki/1706855 to your computer and use it in GitHub Desktop.
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
<!DOCTYPE html> | |
<head> | |
<title>Rx for JavaScript Rocks!</title> | |
<script type="text/javascript"> | |
function throttleInput (element, onNext) { | |
var delay = function (action) { | |
return window.setTimeout(function () { | |
action(); | |
}, 500); | |
} | |
, cancelToken | |
, onkeyup = function (event) { | |
var content = element.value; | |
window.clearTimeout(cancelToken); | |
cancelToken = delay(function () { | |
if (content === element.value && content.length > 2) { | |
onNext(content); | |
} | |
}); | |
}; | |
element.addEventListener('keyup', onkeyup, false); | |
} | |
function initialize () { | |
var textInput = document.getElementById('textInput'); | |
throttleInput(textInput, function (content) { | |
var element = document.createElement('li'); | |
element.innerHTML = content; | |
document.getElementById('results').appendChild(element); | |
}); | |
} | |
</script> | |
</head> | |
<body style="font-family: Consolas, monospace; overflow: hidden" onload="initialize();"> | |
<input type="text" id="textInput"></input> | |
<ul id="results"></ul> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment