vue2/3 防抖

//vue2 optionsAPI
data:{
	timeout:null
},
methods:{
	debounceFn(val){
	 if(this.timeout !== null){
	 	clearTimeout(this.timeout);
	}
	this.timeout  = setTimeout(() => {
		console.log(val)
	},200)
}
//vue3 compositionAPI
const debounce = (fn, wait) {
    let timeout = null
    return function () {
        if (timeout) {
            clearTimeout(timeout)
        }
        timeout = setTimeout(() => fn(), wait)
    }
}
const debounceFn = debounce((val) => {
        console.log(val)
}, 200)
posted @ 2023-10-13 14:50  Chaplink  阅读(52)  评论(0编辑  收藏  举报