We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 09c5e39 commit 7443e50Copy full SHA for 7443e50
2627-debounce.js
@@ -0,0 +1,31 @@
1
+/*
2
+2627. Debounce
3
+
4
+Submitted: December 8, 2024
5
6
+Runtime: 58 ms (beats 62.37%)
7
+Memory: 49.86 MB (beats 8.77%)
8
+*/
9
10
+/**
11
+ * @param {Function} fn
12
+ * @param {number} t milliseconds
13
+ * @return {Function}
14
+ */
15
+function debounce(fn, t) {
16
+ var last_call = -2000;
17
+ var timeoutId = null;
18
+ return (...args) => {
19
+ clearTimeout(timeoutId);
20
+ if (last_call < Date.now()) {
21
+ timeoutId = setTimeout(f = () => { fn(...args) }, t);
22
+ }
23
24
+};
25
26
27
+ * const log = debounce(console.log, 100);
28
+ * log('Hello'); // cancelled
29
30
+ * log('Hello'); // Logged at t=100ms
31
0 commit comments