-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebounce.ts
More file actions
51 lines (38 loc) · 1000 Bytes
/
debounce.ts
File metadata and controls
51 lines (38 loc) · 1000 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { Effect, Duration } from "effect";
import { type DurationInput } from "effect/Duration";
const debounce = <A extends any[], O>(fn: (...args: A) => O, time: number) => {
let timeout: NodeJS.Timeout;
return (...args: A) => {
clearTimeout(timeout);
timeout = setTimeout(() => fn(...args), time);
};
};
const debounce2 = <A extends any[], O>(
fn: (...args: A) => O,
duration: DurationInput
) => {
let timeout: NodeJS.Timeout;
return (...args: A) => {
clearTimeout(timeout);
timeout = setTimeout(() => fn(...args), Duration.toMillis(duration));
};
};
const testFn = (a: number, b: number) => {
console.log("testFn", a, b);
};
debounce(testFn, 1000)(1, 2);
debounce2(testFn, "1 second")(1, 2);
// function myFunction() {
// return "Hello";
// }
// console.log(myFunction);
sayHello(); // Works!
function sayHello() {
console.log("Hello");
}
console.log(typeof foo);
console.log(typeof bar);
var foo = "hello";
function bar() {
return "world";
}