mirror of
https://github.com/devenperez/leetcode.git
synced 2026-06-13 14:57:08 +00:00
19 lines
421 B
JavaScript
19 lines
421 B
JavaScript
/**
|
|
* @param {Function} fn
|
|
* @param {number} t milliseconds
|
|
* @return {Function}
|
|
*/
|
|
var debounce = function(fn, t) {
|
|
let current;
|
|
return function(...args) {
|
|
if (current) clearTimeout(current)
|
|
current = setTimeout(() => fn(...args), t)
|
|
}
|
|
};
|
|
|
|
/**
|
|
* const log = debounce(console.log, 100);
|
|
* log('Hello'); // cancelled
|
|
* log('Hello'); // cancelled
|
|
* log('Hello'); // Logged at t=100ms
|
|
*/ |