Skip to content

Instantly share code, notes, and snippets.

@crookm
Created July 5, 2018 22:23
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 crookm/3f65504a072d8c6277526972aed3094e to your computer and use it in GitHub Desktop.
Save crookm/3f65504a072d8c6277526972aed3094e to your computer and use it in GitHub Desktop.
export default class API {
constructor() {
/**
* This is the function that should be called in the
* actual application.
*/
this.apiFunc = this._debounce(this._apiFunc, 1000 * 2);
}
/**
* The debounce function, named as a 'private' method to
* indicate it shouldn't be executed outside the class.
*
* @param func: the function to execute, passed as anonymous
* @param wait: how long uninterrupted until it should be executed
* @param immediate: if true, just execute now
*/
_debounce(func, wait, immediate) {
var timeout;
return function() {
var context = this;
var args = arguments;
var later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
var callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
}
/**
* Your api function, named as a 'private' method to
* indicate it shouldn't be executed outside the class.
*/
_apiFunc(your, params, here) {
console.log("your debounced api function here!");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment