Skip to content

Instantly share code, notes, and snippets.

@mattpodwysocki
Created January 30, 2012 21:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattpodwysocki/1706855 to your computer and use it in GitHub Desktop.
Save mattpodwysocki/1706855 to your computer and use it in GitHub Desktop.
<!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