uniapp中防抖函数debounce的使用

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) 
            },

 

posted @ 2024-06-17 15:36  牛腩  阅读(11)  评论(0编辑  收藏  举报