mirror of
https://github.com/devenperez/leetcode.git
synced 2026-06-13 14:57:08 +00:00
Time: 70 ms (6.24%), Space: 49 MB (83.2%) - LeetHub
This commit is contained in:
@@ -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
|
||||
*/
|
||||
Reference in New Issue
Block a user