/* eslint-disable */ /** * This function returns a function that, as long as it continues to be invoked, will not be triggered. * The function will be called after it stops being called for N milliseconds. * If immediate is passed as an argument to the function, the function triggers immediately * and then waits for the interval before being called again. * Sample: * var returnedFunction = debounce(function() { * // The function's code * }, 250, false); * window.addEventListener('resize', returnedFunction); * @param func Function to be called * @param wait Timeout before calling a function in milliseconds * @param immediate Sign of immediate function call */ export function debounce(func: Function, wait: number, immediate: boolean) { let timeout: NodeJS.Timeout | null = null return (...args: any[]) => { const later = function () { timeout = null if (!immediate) { // @ts-ignore func.call(this, ...args) } } const callNow = immediate && !timeout timeout && clearTimeout(timeout) timeout = setTimeout(later, wait) if (callNow) { // @ts-ignore func.call(this, ...args) } } }