(func, time)
| 21 | } |
| 22 | |
| 23 | export function debounce(func, time) { |
| 24 | let startTime; |
| 25 | let timer; |
| 26 | let callback; |
| 27 | time = Math.max(0, +time || 0); |
| 28 | function checkTime() { |
| 29 | timer = null; |
| 30 | if (performance.now() >= startTime) callback(); |
| 31 | else checkTimer(); |
| 32 | } |
| 33 | function checkTimer() { |
| 34 | if (!timer) { |
| 35 | const delta = startTime - performance.now(); |
| 36 | timer = setTimeout(checkTime, delta); |
| 37 | } |
| 38 | } |
| 39 | function debouncedFunction(...args) { |
| 40 | startTime = performance.now() + time; |
| 41 | callback = () => { |
| 42 | callback = null; |
| 43 | func.apply(this, args); |
| 44 | }; |
| 45 | checkTimer(); |
| 46 | } |
| 47 | return debouncedFunction; |
| 48 | } |
| 49 | |
| 50 | export function throttle(func, time) { |
| 51 | let lastTime = 0; |
no outgoing calls
no test coverage detected