Home Reference Source Repository

src/common/util/fn.js

/**
* Copyright 2016 Baidu Inc. All rights reserved.
*
* @file function 相关的小工具
* @author leon <[email protected]>
*/


export function throttle(func, wait, options) {

    let timeout;
    let context;
    let args;
    let result;
    let previous = 0;

    if (!options) {
        options = {};
    }

    const later = function () {

        previous = options.leading === false ? 0 : new Date().getTime();
        timeout = null;
        result = func.apply(context, args);

        if (!timeout) {
            context = args = null;
        }

    };

    const throttled = function (...argus) {

        const now = new Date();

        if (!previous && options.leading === false) {
            previous = now;
        }

        const remaining = wait - (now - previous);

        context = this;
        args = argus;

        if (remaining <= 0 || remaining > wait) {
            if (timeout) {
                clearTimeout(timeout);
                timeout = null;
            }
            previous = now;
            result = func.apply(context, args);
            if (!timeout) {
                context = args = null;
            }
        }
        else if (!timeout && options.trailing !== false) {
            timeout = setTimeout(later, remaining);
        }

        return result;
    };

    throttled.cancel = function () {
        clearTimeout(timeout);
        previous = 0;
        timeout = context = args = null;
    };

    return throttled;

}