Total Complexity | 9 |
Complexity/F | 1.5 |
Lines of Code | 41 |
Function Count | 6 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | export function throttle(callback, scope, limit = 100, options = {}) { |
||
2 | |||
3 | options = { |
||
4 | leading: true, |
||
5 | trailing: false, |
||
6 | ...options |
||
7 | }; |
||
8 | |||
9 | let wait = false; |
||
10 | let skip = false; |
||
11 | |||
12 | if (options.leading === false) { |
||
13 | skip = true; |
||
14 | } |
||
15 | |||
16 | const later = debounce(function dodebounce() { |
||
17 | if (options.trailing) { |
||
18 | callback.apply(scope, arguments); |
||
19 | } |
||
20 | if (options.leading === false) { |
||
21 | skip = true; |
||
22 | } |
||
23 | }, limit + limit * 0.15); |
||
24 | |||
25 | return function dothrottle() { |
||
26 | |||
27 | if (!wait && !skip) { |
||
28 | callback.apply(scope, arguments); |
||
29 | wait = true; |
||
30 | setTimeout(() => { |
||
31 | wait = false; |
||
32 | if (later) { |
||
33 | later.apply(scope, arguments); |
||
34 | } |
||
35 | }, limit); |
||
36 | } |
||
37 | else if (!wait && skip) { |
||
38 | wait = true; |
||
39 | setTimeout(() => { |
||
40 | skip = false; |
||
41 | wait = false; |
||
42 | if (later) { |
||
75 |