-
Notifications
You must be signed in to change notification settings - Fork 0
/
execUtils.js
50 lines (42 loc) · 1.27 KB
/
execUtils.js
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
/*---------------------------------------------------------------------------------------*/
export const nextFrame = (fn) =>
//https://dev.to/nikitadmitr/the-other-side-of-using-requestanimationframe-4jk6
requestAnimationFrame(() => {
requestAnimationFrame(fn);
});
/*---------------------------------------------------------------------------------------*/
export const softExec = (fn, wait = 5000) => {
if ("requestIdleCallback" in window) {
requestIdleCallback(fn, {
timeout: wait,
});
} else if ("requestAnimationFrame" in window) {
nextFrame(fn);
} else {
setTimeout(fn, 1);
}
};
/*---------------------------------------------------------------------------------------*/
export const debounce = (fn, timeout = 300) => {
let timer;
return (...args) => {
clearTimeout(timer);
timer = setTimeout(() => {
fn.apply(this, args);
}, timeout);
};
};
/*---------------------------------------------------------------------------------------*/
export const throttle = (fn, timeout = 300) => {
let waiting = false;
return (...args) => {
if (!waiting) {
fn.apply(this, args);
waiting = true;
setTimeout(() => {
waiting = false;
}, timeout);
}
};
};
/*---------------------------------------------------------------------------------------*/