Create a gist now

Instantly share code, notes, and snippets.

// Query the service with the input
function handleText (text) {
// Query the service via a Promise API
queryService(text).then(function (results) {
// Handle completion
}, function (err) {
// Handle errors
});
}
function throttleDistinct (el, event, timeout, onComplete) {
var cancelToken;
// Create delay function to perform action at specified time
var delay = function (action) {
return window.setTimeout(function () {
action();
}, timeout);
};
// Handle event logic
var onEvent = function (e) {
var content = el.value;
// Delay our call
delay( function () {
// Check if distinct and a certain length
if (content === el.value && content.length > 2) {
// Now yield the value
onComplete(content);
}
});
};
element.addEventListener(event, onEvent, false);
}
// Query the service throttled
var element = document.querySelector('#textinput');
throttleDistinct(element, 'keyup', 500, handleText);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment