/**
* 节流函数
* @param {Function} fn 回调函数
* @param {Number} delay 间隔时间
* @param {Boolean} immediate 回调函数是否立即执行
* 返回二位数组中的某列的值
*/
function throttle(fn, delay, immediate = true) {
if (immediate) {
let nowTime;
return function () {
if (!nowTime || Date.now() - nowTime >= delay) {
fn.apply(null, arguments)
nowTime = Date.now();
}
}
} else {
let timer;
return function () {
if (timer) {
return;
}
timer = setTimeout(() => {
fn.apply(null, arguments);
timer = null;
}, delay)
}
}
}
* 节流函数
* @param {Function} fn 回调函数
* @param {Number} delay 间隔时间
* @param {Boolean} immediate 回调函数是否立即执行
* 返回二位数组中的某列的值
*/
function throttle(fn, delay, immediate = true) {
if (immediate) {
let nowTime;
return function () {
if (!nowTime || Date.now() - nowTime >= delay) {
fn.apply(null, arguments)
nowTime = Date.now();
}
}
} else {
let timer;
return function () {
if (timer) {
return;
}
timer = setTimeout(() => {
fn.apply(null, arguments);
timer = null;
}, delay)
}
}
}
全部评论
相关推荐

点赞 评论 收藏
分享
02-21 17:03
大连理工大学 运营 

点赞 评论 收藏
分享
01-14 20:42
南昌航空大学科技学院 前端工程师 点赞 评论 收藏
分享
02-26 09:38
门头沟学院 后端 点赞 评论 收藏
分享