Created
July 5, 2018 22:23
-
-
Save crookm/3f65504a072d8c6277526972aed3094e to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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