JS请求节流
JS请求节流
不废话 撸代码
节流器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23// 对函数进行 节流
function throttle (fn, interval = 500) {
let timer = null;
let firstTime = true;
return function () {
let args = arguments;
if (firstTime) {
// 第一次加载
fn.apply(this, args);
return firstTime = false;
}
if (timer) {
// 定时器正在执行中,跳过
return;
}
timer = setTimeout(() => {
clearTimeout(timer);
timer = null;
fn.apply(this, args);
}, interval);
};
}初始化节流器
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25const throttleFunc=throttle(function (field) {
var loadIndex = layer.load(1);
$.ajax({
type: 'POST',
url: '/cms/teachers/saveTeacherInfo',
async: false,
data: data.field,
success: function (result) {
if (result.status==1) {
layer.close(loadIndex);
layer.msg('保存成功');
canJump = 1;
if (!isJump) {
$('.cancel').trigger('click');
}
} else {
$('button[lay-filter="saveTeacherInfo"]').attr("disabled", false).removeClass("layui-disabled");
layer.close(loadIndex);
layer.msg(result.msg);
}
}
});
return false;
},1000);使用节流器
1
throttleFunc(data.field);
JS请求节流
https://www.ivan.fun/posts/12c97b7c/