bigbluebutton-Github/bigbluebutton-html5/imports/utils/throttle.js
2023-03-02 10:25:08 -03:00

29 lines
647 B
JavaScript

export function throttle(func, delay, options = {}) {
let timeoutId;
let lastExecTime = 0;
let leadingExec = true;
const { leading = true, trailing = true } = options;
return function () {
const context = this;
const args = arguments;
const elapsed = Date.now() - lastExecTime;
function execute() {
func.apply(context, args);
lastExecTime = Date.now();
}
if (leadingExec && leading) {
execute();
leadingExec = false;
} else if (!timeoutId && trailing) {
timeoutId = setTimeout(function () {
execute();
timeoutId = null;
}, delay - elapsed);
}
}
}