@vime/core
Version:
Customizable, extensible, accessible and framework agnostic media player.
19 lines (18 loc) • 579 B
JavaScript
import { isUndefined } from './unit';
export const debounce = (func, wait = 1000, immediate = false) => {
let timeout;
return function executedFunction(...args) {
// eslint-disable-next-line @typescript-eslint/no-this-alias
const context = this;
const later = function delayedFunctionCall() {
timeout = undefined;
if (!immediate)
func.apply(context, args);
};
const callNow = immediate && isUndefined(timeout);
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow)
func.apply(context, args);
};
};