首页 > 其他 > 详细

节流和防抖函数

时间:2018-12-27 21:17:31      阅读:136      评论:0      收藏:0      [点我收藏+]

/**
 * 节流函数
 * @param {Funtion} method 回调函数
 * @param {Object} context 上下文地址
 * @param {number} delay 延迟时间ms
 */
function throttle(method, context, delay) {
    delay = delay || 500;
    var currentDate = new Date();
    method.startTime = method.startTime || 0;
    if (currentDate - method.startTime > delay) {
        method.call(context);
        method.startTime = new Date();
    }
    else {
        clearTimeout(method.timer);
        method.timer = setTimeout(function () {
            throttle(method, context, delay);
        }, 50);
    }
}


/**
 * 防抖函数
 * @param {Funtion} method 回调函数
 * @param {Object} context 上下文地址
 * @param {number} delay 延迟时间ms
 */
function debound(method, context, delay) {
    delay = delay || 200;
    clearTimeout(context.deboundId);
    context.deboundId = setTimeout(function () {
        method.call(context);
    }, delay);
}

节流和防抖函数

原文:https://www.cnblogs.com/jerrypig/p/10187368.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!