UNPKG

741 BJavaScriptView Raw
1/**
2 * Created by liuzhengdong on 2018/4/3.
3 * 存放公共方法
4 */
5module.exports = {
6 /**
7 * 函数节流
8 * @param fn 函数
9 * @param delay 多久执行一次
10 * @param mustRunDelay 执行时间间隔
11 * @return {Function}
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