美文网首页JavaScript
window--resize防抖

window--resize防抖

作者: 开着五菱宏光的小白 | 来源:发表于2020-07-20 19:09 被阅读0次
    
    function debounce(operate, delay) {
        let time = null
        let timer = null
        let newTime = null
        function task() {
            newTime = +new Date()
            if(newTime - time < delay){
                timer = setTimeout(task, delay)
            }else {
                operate()
                timer = null
            }
            time = newTime
        }
        return function () {
            // 更新时间戳
            time = +new Date()
            if(!timer){
                timer = setTimeout(task, delay)
            }
        }
    }
    
    function task() {
            dosomething...
    }
    
    window.addEventListener('resize', debounce(task, 300), false)
    
    

    相关文章

      网友评论

        本文标题:window--resize防抖

        本文链接:https://www.haomeiwen.com/subject/jogykktx.html