防抖

function debounce(func, delay) {
    let timerId;
  
    return function() {
        clearTimeout(timerId); // 清除之前设置的计时器
      
        const context = this;
        const args = arguments;
        
        timerId = setTimeout(() => {
            func.apply(context, args);
        }, delay);
    };
}
 
// 使用示例
const myFunction = () => {
    console.log("Hello World!");
};
 
const debouncedFunc = debounce(myFunction , 1000); // 创建防抖后的函数
 
 
 
//微信小程序
function debounce(fn,interval) {
    var timer
    var gaptime = 1000
    return function () {
        clearTimeout(timer)
        var context = this
        var args = arguments
    timer = setTimeout(function () {
    fn.call(context,args)
        console.log(fn,'11111111111111')
    },gaptime)

    }
}
 
module.exports = {
    debounce,
}
 
//页面js
import tool from '../../../utils/util'
    Clik:tool.debounce(function name(evt) {
        this.signin()    //要调用的方法
    }),
posted @ 2024-01-30 09:17  巳蛇  阅读(26)  评论(0)    收藏  举报