首页 > 其他分享 >uniapp中防抖函数debounce的使用

uniapp中防抖函数debounce的使用

时间:2024-06-17 15:46:27浏览次数:9  
标签:uniapp index debounce 中防抖 js selectDate

uniapp中防抖函数debounce的使用 分段控件u-subsection每次点击一个tab的时候都会ajax访问一次接口取列表数据的,这时如果有人快速在多个分段间快速点击的话,每次点击都会访问接口的,网上找了好多资料,终于找到了这个玩意。。。记得以前也弄过这个的。。不过当时没有记下来,现在记下来,以做备用 common/js目录下建立debounce.js文件

// 防抖  
let t = null;  
export const debounce = function( fn, delay ) {  
    if( t !== null ) {  
        clearTimeout(t);  
    }  
    t = setTimeout(() => {  
        fn()  
    },delay)  
}
使用:
    //要先导入debounce函数
     import {debounce} from '@/common/js/debounce.js';
       // 分段器切换
            xuanze(index) {
                this.current = index;
                if (index == 0) {
                    this.selectDate = getDate()
                } else if (index == 1) {
                    this.selectDate = getDate(1)
                } else {
                    this.selectDate = getDate(2)
                }
             //调用防抖函数 
             debounce(() =>{  
                 this.getDoctorlist() //请求列表
             },600) 
            },

 

标签:uniapp,index,debounce,中防抖,js,selectDate
From: https://www.cnblogs.com/niunan/p/18252532

相关文章