Skip to content

Commit d74cffc

Browse files
authored
Create 2627-debounce.js
1 parent c071774 commit d74cffc

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

javascript/2627-debounce.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {Function} fn
3+
* @param {number} t milliseconds
4+
* @return {Function}
5+
*/
6+
var debounce = function (fn, t) {
7+
let timeoutHandle;
8+
return function (...args) {
9+
clearTimeout(timeoutHandle);
10+
timeoutHandle = setTimeout(() => fn(...args), t);
11+
}
12+
};
13+
14+
/**
15+
* const log = debounce(console.log, 100);
16+
* log('Hello'); // cancelled
17+
* log('Hello'); // cancelled
18+
* log('Hello'); // Logged at t=100ms
19+
*/

0 commit comments

Comments
 (0)