-
Notifications
You must be signed in to change notification settings - Fork 77
/
others.js
73 lines (64 loc) · 1.57 KB
/
others.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
* @authors : qieguo
* @date : 2017/3/6 0006
* @description :
*/
// 节流throttle,多次触发但只执行一部分,(恒时间间距执行)
function throttle(method, threshold, ctx) {
let timer = null;
return function () {
const args = [].slice.call(arguments);
if (!timer) {
timer = setTimeout(function () {
timer = null;
method.apply(ctx, args);
}, threshold);
}
}
}
// 防抖debounce, 多次触发但只执行一次,(时间差大于阈值才执行)
function debounce(method, threshold, ctx) {
let timer = null;
return function () {
const args = [].slice.call(arguments);
timer && clearTimeout(timer);
timer = setTimeout(function () {
method.apply(ctx, args);
}, threshold);
};
}
// 轮循函数
// usage: wait(fn.bind(ctx, ...args), 10000);
function wait(fn, timeout, tick) {
timeout = timeout || 5000;
tick = tick || 250;
var timeoutTimer = null;
var execTimer = null;
return new Promise(function (resolve, reject) {
timeoutTimer = setTimeout(function () {
clearTimeout(execTimer);
reject(new Error('polling fail because timeout'));
}, timeout);
tickHandler(fn);
function tickHandler(fn) {
var ret = fn();
if (!ret) {
execTimer = setTimeout(function () {
tickHandler(fn);
}, tick)
} else {
clearTimeout(timeoutTimer);
resolve();
}
}
});
}
var n = 1;
wait(function () {
console.log(n++);
return n > 10;
}, 2000, 300).then(function () {
console.log('===== end ====')
}).catch(function (err) {
console.error('error', err);
});