1 |
|
2 |
|
3 |
|
4 |
|
5 | module.exports = {
|
6 | |
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 | throttle: function (fn, delay, mustRunDelay) {
|
14 | let timer = null
|
15 | let t_start
|
16 | return function () {
|
17 | let context = this, args = arguments, t_curr = +new Date()
|
18 | clearTimeout(timer)
|
19 | if (!t_start) {
|
20 | t_start = t_curr
|
21 | }
|
22 | if (t_curr - t_start >= mustRunDelay) {
|
23 | fn.apply(context, args)
|
24 | t_start = t_curr
|
25 | }
|
26 | else {
|
27 | timer = setTimeout(function () {
|
28 | fn.apply(context, args)
|
29 | }, delay)
|
30 | }
|
31 | }
|
32 | },
|
33 | } |
\ | No newline at end of file |