Skip to content

Instantly share code, notes, and snippets.

@coderatchet
Last active August 14, 2016 22:06
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 coderatchet/5bcbe1eae2e47154258e5946462f2cc8 to your computer and use it in GitHub Desktop.
Save coderatchet/5bcbe1eae2e47154258e5946462f2cc8 to your computer and use it in GitHub Desktop.
goog.async.Debouncer usage
//after a little messing around, I found out how to use the goog.async.Debouncer properly
goog.require('goog.async.Debouncer');
goog.require('goog.events.InputHandler');
var usernameField_ = /* something like a textfield */
var thingThatShouldHappenOnceAfterInterval = function(){ /* ... */ }
var inputHandler = new goog.events.InputHandler(userNameField_);
var eventHandler_ = /* @type goog.events.EventHandler */
/* where the magic happens */
var debouncer = new goog.async.Debouncer(function(){
this.thingThatShouldHappenOnceAfterInterval();
console.log("do the thing only once eventually!");
}, 5000 /* millis */ , this /* function context */);
eventHandler.listen(inputHandler, goog.events.EventType.INPUT, function(){
debouncer.fire(); // lets the debouncer know the event happened, and restarts the timer for the handler func.
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment