Home Reference Source Repository

src/services/debounceTime.js

/**
 * Execute a function given a delay time.
 * @method debounceTime
 * @param {function} f - invoked function.
 * @param {number} d - a delay time.
 * @param {boolean} [immediate = true] immediate - first func call is immediate if true.
 * @return {function(...args:any[])=>void}
 */
export function debounceTime(f, d, immediate = true) {
    let timeout;
    return (...args) => {
        const later = () => {
            timeout = null;
            if (!immediate)
                f.apply(this, args);
        };
        const callNow = immediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, d);
        if (callNow)
            f.apply(this, args);
    };
}
//Copyright (c) 2017 Alex Tranchenko. All rights reserved.