-
-
Save Krinkle/a9ec69e08538e85df1b11a8fee872e02 to your computer and use it in GitHub Desktop.
This file contains 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
/*! Author: Timo Tijhof (2019) | License: Public domain. */ | |
/** | |
* Wrap the function and debounce calls to it for a specified duration. | |
* | |
* @param {Number} delay | |
* @param {Function} callback | |
* @return {Function} | |
*/ | |
function debounce( delay, callback ) { | |
var timeout; | |
return function debounced() { | |
clearTimeout( timeout ); | |
timeout = setTimeout( Function.prototype.apply.bind( callback, this, arguments ), delay ); | |
}; | |
} | |
/** | |
* Make the function async and ignore successive calls until it's finished. | |
* | |
* @param {Function} callback | |
* @return {Function} | |
*/ | |
function throttle( callback ) { | |
var pending, ctx, args; | |
return function() { | |
ctx = this, args = arguments; // last call before timeout should win | |
if ( !pending ) { | |
pending = setTimeout( function () { | |
pending = null; | |
callback.apply( ctx, args ); | |
} ); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Net costs for module responses: