小程序 节流函数 (防止按钮重复点击)

时间:2019-12-18 14:24:36   收藏:0   阅读:170
function throttle(fn, gapTime) {
    if (gapTime == null || gapTime == undefined) {
        gapTime = 1500
    }
 
    let _lastTime = null
    // 返回新的函数
    return function () {
        let _nowTime = + new Date()
        if (_nowTime - _lastTime > gapTime || !_lastTime) {
            fn.apply(this, arguments)   //将this和参数传给原函数
            _lastTime = _nowTime
        }
    }
}

module.exports = {
    throttle: throttle
}
    but_: util.throttle(function(e) {
        let that = this
        var data;
        //执行函数内容
    }, 1000),        
评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!