Time: 70 ms (6.24%), Space: 49 MB (83.2%) - LeetHub

This commit is contained in:
Deven
2024-11-21 19:43:33 -05:00
parent ca91da0a84
commit b22f362ce6
+19
View File
@@ -0,0 +1,19 @@
/**
* @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
*/