routie dev init since i didn't adhere to any proper guidance up until now
This commit is contained in:
+39
@@ -0,0 +1,39 @@
|
||||
export function throttle(fn, delay, options = {
|
||||
leading: true,
|
||||
trailing: true
|
||||
}) {
|
||||
let timeoutId = 0;
|
||||
let lastExec = 0;
|
||||
let throttling = false;
|
||||
let start = 0;
|
||||
function clear() {
|
||||
clearTimeout(timeoutId);
|
||||
throttling = false;
|
||||
start = 0;
|
||||
}
|
||||
const wrap = (...args) => {
|
||||
clearTimeout(timeoutId);
|
||||
const now = Date.now();
|
||||
if (!start) start = now;
|
||||
const elapsed = now - Math.max(start, lastExec);
|
||||
function invoke() {
|
||||
lastExec = Date.now();
|
||||
timeoutId = setTimeout(clear, delay);
|
||||
fn(...args);
|
||||
}
|
||||
if (!throttling) {
|
||||
throttling = true;
|
||||
if (options.leading) {
|
||||
invoke();
|
||||
}
|
||||
} else if (elapsed >= delay) {
|
||||
invoke();
|
||||
} else if (options.trailing) {
|
||||
timeoutId = setTimeout(invoke, delay - elapsed);
|
||||
}
|
||||
};
|
||||
wrap.clear = clear;
|
||||
wrap.immediate = fn;
|
||||
return wrap;
|
||||
}
|
||||
//# sourceMappingURL=throttle.js.map
|
||||
Reference in New Issue
Block a user