xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

js debounce in action

js debounce in action


// const log = console.log;

// function debounce (callback, timer = 1000) {
//     if(typeof callback === 'function') {
//         let timerId = null;
//         return function () {
//             clearTimeout(timerId);
//             id = setTimeout(() => {
//                 callback();
//             }, timer);
//         };
//     } else {
//         throw new Error('请传入 function 作为回调函数!');
//     }
// }

function debounce (func, wait, immediate) {
    let timeout;
    return function() {
        let context = this, args = arguments;
        let later = function() {
            timeout = null;
            if (!immediate) func.apply(context, args);
        };
        let callNow = immediate && !timeout;
        clearTimeout(timeout);
        timeout = setTimeout(later, wait);
        if (callNow) func.apply(context, args);
    };
};

    autoOperateDetect (cb) {
        window.addEventListener('click', debounce(function() {
            this.emitMouseEvent(cb, this.uid, 'click', '鼠标移动 click');
        }, 1000));
        window.addEventListener('dblclick', debounce(function() {
            this.emitMouseEvent(cb, this.uid, 'dblclick', '鼠标移动 dblclick');
        }, 1000));
        window.addEventListener('mouseup', debounce(function() {
            this.emitMouseEvent(cb, this.uid, 'mouseup', '鼠标移动 mouseup');
        }, 1000));
        window.addEventListener('mousedown', debounce(function() {
            this.emitMouseEvent(cb, this.uid, 'mousedown', '鼠标移动 mousedown');
        }, 1000));
        window.addEventListener('mousemove', debounce(function() {
            this.emitMouseEvent(cb, this.uid, 'mousemove', '鼠标移动 mousemove');
        }, 1000));
        window.addEventListener('keydown', debounce(function() {
            this.emitKeyboardEvent(cb, this.uid, 'keydown', '键盘输入 keydown');
        }, 1000));
        window.addEventListener('keyup', debounce(function() {
            this.emitKeyboardEvent(cb, this.uid, 'keyup', '键盘输入 keyup');
        }, 1000));
        window.addEventListener('keypress', debounce(function() {
            this.emitKeyboardEvent(cb, this.uid, 'keypress', '键盘输入 keypress');
        }, 1000));
        // window.addEventListener('click', (e) => {
        //     this.emitMouseEvent(cb, this.uid, 'click', '鼠标移动 click');
        // });
        // window.addEventListener('dblclick', (e) => {
        //     this.emitMouseEvent(cb, this.uid, 'dblclick', '鼠标移动 dblclick');
        // });
        // window.addEventListener('mouseup', (e) => {
        //     this.emitMouseEvent(cb, this.uid, 'mouseup', '鼠标移动 mouseup');
        // });
        // window.addEventListener('mousedown', (e) => {
        //     this.emitMouseEvent(cb, this.uid, 'mousedown', '鼠标移动 mousedown');
        // });
        // window.addEventListener('mousemove', (e) => {
        //     this.emitMouseEvent(cb, this.uid, 'mousemove', '鼠标移动 mousemove');
        // });
        // window.addEventListener('keydown', (e) => {
        //     this.emitKeyboardEvent(cb, this.uid, 'keydown', '键盘输入 keydown');
        // });
        // window.addEventListener('keyup', (e) => {
        //     this.emitKeyboardEvent(cb, this.uid, 'keyup', '键盘输入 keyup');
        // });
        // window.addEventListener('keypress', (e) => {
        //     this.emitKeyboardEvent(cb, this.uid, 'keypress', '键盘输入 keypress');
        // });
    }

refs

https://davidwalsh.name/javascript-debounce-function



©xgqfrms 2012-2020

www.cnblogs.com 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2021-05-08 11:57  xgqfrms  阅读(47)  评论(1编辑  收藏  举报