MCPcopy
hub / github.com/mynane/PDF / throttle

Function throttle

设计模式学习/函数节流方法实现/throttle.js:1–31  ·  view source on GitHub ↗
(fn, interval )

Source from the content-addressed store, hash-verified

1var throttle = function (fn, interval ) {
2 // 保存需要被延迟执行的函数引用
3 var __self = fn,
4 // 定时器
5 timer,
6 // 是否是第一次调用
7 firstTime = true;
8
9 return function () {
10 var args = arguments,
11 __me = this;
12
13 if(firstTime) {
14 // 如果第一次调用, 不需要延迟执行
15 __self.apply(__me, args);
16 return firstTime = false;
17 }
18
19 if (timer) {
20 // 如果定时器还在,说明前一次延迟执行还没有完成
21 return false;
22 }
23
24 // 延迟一段时间执行
25 timer = setTimeout(function(){
26 clearTimeout(timer);
27 timer = null;
28 __self.apply(__me, args);
29 },interval || 500);
30 };
31};

Callers 1

lazyLoadImg.jsFile · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…