SOURCE

/**
 * 错误的实现
 */
// function debounce(func, wait) {
//     let lastTime = new Date().valueOf();
//     return function() {
//         const deltaTime = new Date().valueOf() - lastTime;
//         if (deltaTime >= wait) {
//             func();
//         }
//         lastTime = new Date().valueOf();
//     }
// }

/**
 * 正确的实现
 */
function debounce(func, wait) {
    let timer;
    return function() {
        clearTimeout(timer);
        timer = setTimeout(func, wait);
    }
}

function print() {
    console.log(new Date())
    console.log('print ' + this.name);
}

var name = 'Sharlok'
var movie = { name: 'Spider Man' }

console.log(new Date())
const newfunc = debounce(print.bind(window), 1000);
let count = 0;
let theTimer = setInterval(()=>{
    if (count >= 2) clearInterval(theTimer);
    newfunc();
    count ++;
}, 200);

const newfunc1 = _.debounce(print.bind(movie), 1000);
// setInterval(newfunc1, 2000);
console 命令行工具 X clear

                    
>
console