SOURCE

// func是用户传入需节流的函数
// wait是等待时间

const throttle = (func, wait = 50) => {
    // 上一次执行该函数的时间
    let lastTime = 0
    return function(...args) {
        // 当前时间
        let now = +new Date()
        // 将当前时间和上一次执行函数时间对比
        // 如果差值大于设置的等待时间就执行函数
        if(now - lastTime > wait) {
            lastTime = now
            func.apply(this, args)
        }
    }
}



setInterval(
  throttle(() => {
    console.log(1)
  }, 1000),
  1
)

// const throttle = (func, wait = 50) => {
//     let lastTime = 0
//     return function(...args) {
//         let now = +new Date()
//         if(now - lastTime > wait) {
//             lastTime = now
//             func.apply(this, args)
//         }
//     }
// }
console 命令行工具 X clear

                    
>
console