Skip to content

Commit 0052f94

Browse files
day 18 task complete
1 parent c4718c1 commit 0052f94

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

day18/leetcode2627.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// 2627. Debounce
2+
// URL -> https://leetcode.com/problems/debounce
3+
4+
/**
5+
* @param {Function} fn
6+
* @param {number} t milliseconds
7+
* @return {Function}
8+
*/
9+
var debounce = function (fn, t) {
10+
let timeout;
11+
return function (...args) {
12+
if (timeout) {
13+
clearTimeout(timeout);
14+
}
15+
16+
timeout = setTimeout(() => {
17+
fn(...args);
18+
}, t)
19+
}
20+
};
21+
22+
/**
23+
* const log = debounce(console.log, 100);
24+
* log('Hello'); // cancelled
25+
* log('Hello'); // cancelled
26+
* log('Hello'); // Logged at t=100ms
27+
*/

0 commit comments

Comments
 (0)