mirror of
https://github.com/idanoo/GoScrobble.git
synced 2024-11-24 09:25:15 +00:00
31 lines
698 B
JavaScript
31 lines
698 B
JavaScript
/**
|
|
* Debounce a function to delay invoking until wait (ms) have elapsed since the last invocation.
|
|
* @param {function(...*): *} fn The function to be debounced.
|
|
* @param {number} wait Milliseconds to wait before invoking again.
|
|
* @return {function(...*): void} The debounced function.
|
|
*/
|
|
function debounce(fn, wait) {
|
|
/**
|
|
* A cached setTimeout handler.
|
|
* @type {number | undefined}
|
|
*/
|
|
let timer;
|
|
|
|
/**
|
|
* @returns {void}
|
|
*/
|
|
function debounced() {
|
|
const context = this;
|
|
const args = arguments;
|
|
|
|
clearTimeout(timer);
|
|
timer = setTimeout(function () {
|
|
return fn.apply(context, args);
|
|
}, wait);
|
|
}
|
|
|
|
return debounced;
|
|
}
|
|
|
|
module.exports = debounce;
|