Skip to content

Instantly share code, notes, and snippets.

@kunev
Created February 8, 2013 15:07
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 kunev/4739557 to your computer and use it in GitHub Desktop.
Save kunev/4739557 to your computer and use it in GitHub Desktop.
Custom throttle extender for knockout.js that throttles input but updates the binding immediately when blurring the text field bound to it. Based on the original throttle extender.
ko.extenders.smart_throttle = function(target, timeout) {
target['throttleEvaluation'] = timeout;
target['lastUpdatedValue'] = null;
var writeTimeoutInstance = null;
return ko.computed({
'read': target,
'write': function(value) {
if (value == target()) {
clearTimeout(writeTimeoutInstance);
target(target['lastUpdatedValue']);
return;
}
clearTimeout(writeTimeoutInstance);
target['lastUpdatedValue'] = value;
writeTimeoutInstance = setTimeout(function() {
target(value);
}, timeout);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment