Skip to content

函数节流

throttle源码
ts
export function throttle<T extends any[], R = void>(
    fn: (...argu: T) => R,
    interval: number
) {
    let last: number
    let timer: ReturnType<typeof setInterval> | void
    return function (this: void, ...argu: T) {
        const now = +new Date()
        if (last && now - last < interval) {
            if (timer) {
                clearTimeout(timer)
            }
            timer = setTimeout(() => {
                last = now
                fn.apply(this, argu)
            }, interval)
        } else {
            last = now
            fn.apply(this, argu)
        }
    }
}
export function throttle<T extends any[], R = void>(
    fn: (...argu: T) => R,
    interval: number
) {
    let last: number
    let timer: ReturnType<typeof setInterval> | void
    return function (this: void, ...argu: T) {
        const now = +new Date()
        if (last && now - last < interval) {
            if (timer) {
                clearTimeout(timer)
            }
            timer = setTimeout(() => {
                last = now
                fn.apply(this, argu)
            }, interval)
        } else {
            last = now
            fn.apply(this, argu)
        }
    }
}

源码

查看源码
throttle/index.ts源码
ts
//throttle===== Start
export function throttle<T extends any[], R = void>(
    fn: (...argu: T) => R,
    interval: number
) {
    let last: number
    let timer: ReturnType<typeof setInterval> | void
    return function (this: void, ...argu: T) {
        const now = +new Date()
        if (last && now - last < interval) {
            if (timer) {
                clearTimeout(timer)
            }
            timer = setTimeout(() => {
                last = now
                fn.apply(this, argu)
            }, interval)
        } else {
            last = now
            fn.apply(this, argu)
        }
    }
}
//throttle===== End
//throttle===== Start
export function throttle<T extends any[], R = void>(
    fn: (...argu: T) => R,
    interval: number
) {
    let last: number
    let timer: ReturnType<typeof setInterval> | void
    return function (this: void, ...argu: T) {
        const now = +new Date()
        if (last && now - last < interval) {
            if (timer) {
                clearTimeout(timer)
            }
            timer = setTimeout(() => {
                last = now
                fn.apply(this, argu)
            }, interval)
        } else {
            last = now
            fn.apply(this, argu)
        }
    }
}
//throttle===== End

Released under the MIT License.