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
| export function throttle(func, delay) {
| let lastTime = 0;
|
| return function(...args) {
| const now = Date.now();
|
| if (now - lastTime >= delay) {
| func.apply(this, args);
| lastTime = now;
| }
| }
| }
|
| export function debounceThrottle(func, wait, immediate) {
| let timeout,
| previous = 0;
| return function (...args) {
| const now = Date.now();
| const later = () => {
| timeout = null;
| if (!immediate) func.apply(this, args);
| };
| const callNow = immediate && !timeout;
| clearTimeout(timeout);
| if (now - previous > wait) {
| previous = now;
| if (callNow) func.apply(this, args);
| }
| timeout = setTimeout(later, wait);
| };
| }
|
|