Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

function debounce(fn, wait) {
    var timeout = null;
    return function() {
        if(timeout !== null) 
                clearTimeout(timeout);
        timeout = setTimeout(fn, wait);
    }
}
// 处理函数
function handle() {
    console.log(Math.random()); 
}
// 滚动事件
window.addEventListener('scroll', debounce(handle, 1000));

这个代码用 setup 写时,这个绑定怎么写
下面代码有问题

<div @scroll="debounce(mousewheel,1000)"></div>

setup(){
        function debounce(fn,wait) {
            let timeoutID = null
            return function () {
                if (timeoutID != null) clearTimeout(timeoutID) 
                timeoutID = setTimeout(fn,wait)
            }
        }

        function mousewheel(e){
            console.log(e);
        }
        
                return {
            mousewheel,debounce
        }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
1.3k views
Welcome To Ask or Share your Answers For Others

1 Answer

<div @scroll="mousewheel"></div>

function debounce(fn,wait) {
            let timeoutID = null
            return function () {
                if (timeoutID != null) clearTimeout(timeoutID) 
                timeoutID = setTimeout(fn,wait)
            }
        }

setup(){

        const mousewheel = debounce((e){
            console.log(e);
        }, 1000)
        
                return {
            mousewheel
        }

}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...