MCPcopy Create free account
hub / github.com/violentmonkey/violentmonkey / debounce

Function debounce

src/common/util.js:23–48  ·  view source on GitHub ↗
(func, time)

Source from the content-addressed store, hash-verified

21}
22
23export 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
50export function throttle(func, time) {
51 let lastTime = 0;

Callers 4

index.test.jsFile · 0.90
options.jsFile · 0.90
base.jsFile · 0.90
hookSettingsForUIFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected