// 1.获取输入框
var search = document.querySelector(\\\\\\\".search\\\\\\\");
// 2.监听输入内容,发送ajax请求
// 2.1.定义一个监听函数
var counter = 0;
function searchChange() {
counter++;
console.log(\\\\\\\"发送\\\\\\\"+ counter +\\\\\\\"网络请求\\\\\\\");
}
// 绑定oninput
search.oninput = searchChange
```
测试发现快速输入一个macbook共发送了7次请求,显示我们需要对它进行防抖操作
### 2.2. 第三方库实现
事实上我们可以通过一些第三方库来实现防抖操作:
* lodash
* underscore
**我们这里以lodash为例,我们可以理解成lodash是underscore的升级版**
#### 2.2.1. 安装lodash
lodash的官方:https://lodash.com/
lodash的安装有很多种方式:
* 下载lodash,本地引入;
* 通过CDN直接引入;
* 通过包管理工具(npm)管理安装;
我们这里以CDN为例:
```
```
#### 2.2.2. 使用lodash
下面我们通过lodash中的debounce函数对searchChange函数进行处理:
* debounce函数要求我们传入一个需要处理的函数,并且传入一个delay的时间
* 在delay的时间内没有再次触发事件,才会真正执行函数
* debounce返回一个新的函数,将新的函数设置到oninput事件中
```
// 对searchChange处理
var _searchChange = _.debounce(searchChange, 500);
// 绑定oninput
search.oninput = _searchChange
var _searchChange = _.debounce(searchChange, 500);
// 绑定oninput
search.oninput = _searchChange
var _searchChange = _.debounce(searchChange, 500);
// 绑定oninput
search.oninput = _searchChange
_searchChange = _.debounce(searchChange, 500);
// 绑定oninput
search.oninput = _searchChange
500);
// 绑定oninput
search.oninput = _searchChange
);
// 绑定oninput
search.oninput = _searchChange
// 绑定oninput
search.oninput = _searchChange
// 绑定oninput
search.oninput = _searchChange
// 绑定oninput
search.oninput = _searchChange
search.oninput = _searchChange
search.oninput = _searchChange
```
测试结果如下:
* 当我快速输入macbook时,只会发送一次请求
### 2.3. 自定义防抖函数
作为学习,我们肯定不满足只会用第三方的库,成为一个API程序员。另外为了一个防抖函数引用整个库对最终打包项目的大小也会有影响。
所以,我们要实现一个自己的防抖函数。
* 注意,在整个实现过程中,我尽可能**不采用ES6的语法**;
* 因为如果我们不babel来处理我们的代码,ES6以上的语法会存在兼容性问题;
* 我们封装的防抖和节流函数就不太具备通用性;
#### 2.3.1. 防抖基本功能
防抖函数的核心思路如下:
* 当触发一个函数时,并不会立即执行这个函数,而是会延迟(通过定时器来延迟函数的执行)
* 如果在延迟时间内,有重新触发函数,那么取消上一次的函数执行(取消定时器);
* 如果在延迟时间内,没有重新触发函数,那么这个函数就正常执行(执行传入的函数);
接下来,就是将思路转成代码即可:
* 定义debounce函数要求传入两个参数
* 需要处理的函数fn;
* 延迟时间;
* 通过定时器来延迟传入函数fn的执行
* 如果在此期间有再次触发这个函数,那么clearTimeout取消这个定时器;
* 如果没有触发,那么在定时器的回调函数中执行即可;
```
function debounce(fn, delay) {
var timer = null;
return function() {
if (timer) clearTimeout(timer);
timer = setTimeout(function() {
fn();
}, delay);
}
}
{
var timer = null;
return function() {
if (timer) clearTimeout(timer);
timer = setTimeout(function() {
fn();
}, delay);
}
}
var timer = null;
return function() {
if (timer) clearTimeout(timer);
timer = setTimeout(function() {
fn();
}, delay);
}
}
var timer = null;
return function() {
if (timer) clearTimeout(timer);
timer = setTimeout(function() {
fn();
}, delay);
}
}
var timer = null;
return function() {
if (timer) clearTimeout(timer);
timer = setTimeout(function() {
fn();
}, delay);
}
}
timer = null;
return function() {
if (timer) clearTimeout(timer);
timer = setTimeout(function() {
fn();
}, delay);
}
}
null;
return function() {
if (timer) clearTimeout(timer);
timer = setTimeout(function() {
fn();
}, delay);
}
}
;
return function() {
if (timer) clearTimeout(timer);
timer = setTimeout(function() {
fn();
}, delay);
}
}
return function() {
if (timer) clearTimeout(timer);
timer = setTimeout(function() {
fn();
}, delay);
}
}
return function() {
if (timer) clearTimeout(timer);
timer = setTimeout(function() {
fn();
}, delay);
}
}
return function() {
if (timer) clearTimeout(timer);
timer = setTimeout(function() {
fn();
}, delay);
}
}
function() {
if (timer) clearTimeout(timer);
timer = setTimeout(function() {
fn();
}, delay);
}
}
function() {
if (timer) clearTimeout(timer);
timer = setTimeout(function() {
fn();
}, delay);
}
}
{
if (timer) clearTimeout(timer);
timer = setTimeout(function() {
fn();
}, delay);
}
}
if (timer) clearTimeout(timer);
timer = setTimeout(function() {
fn();
}, delay);
}
}
if (timer) clearTimeout(timer);
timer = setTimeout(function() {
fn();
}, delay);
}
}
if (timer) clearTimeout(timer);
timer = setTimeout(function() {
fn();
}, delay);
}
}
(timer) clearTimeout(timer);
timer = setTimeout(function() {
fn();
}, delay);
}
}
timer = setTimeout(function() {
fn();
}, delay);
}
}
timer = setTimeout(function() {
fn();
}, delay);
}
}
function() {
fn();
}, delay);
}
}
{
fn();
}, delay);
}
}
fn();
}, delay);
}
}
fn();
}, delay);
}
}
}, delay);
}
}
}, delay);
}
}
}
}
}
}
}
}
```
``` #### 2.3.4. 优化立即执行 目前我们的事件触发都要等到delay时间,但是某些场景是用户开始输入时的第一次是立即执行的,后续的输入才需要等待,我们可以如何优化呢? * 我们可以让用户多传入一个参数:leading * 那么第一次就立即执行 * 后来的事件需要等待delay时间执行 * leading为false,或者不传,那么按照上面的防抖进行操作 * leading为true * 我们可以根据是否传入leading进行不同的处理方式 这个代码会一些复杂,在立即执行的地方需要进行更多的操作: ``` function debounce(fn, delay, leading) { var timer = null; leading = leading || false; var handleFn = function() { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } { var timer = null; leading = leading || false; var handleFn = function() { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } var timer = null; leading = leading || false; var handleFn = function() { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } var timer = null; leading = leading || false; var handleFn = function() { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } var timer = null; leading = leading || false; var handleFn = function() { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } timer = null; leading = leading || false; var handleFn = function() { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } null; leading = leading || false; var handleFn = function() { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } ; leading = leading || false; var handleFn = function() { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } leading = leading || false; var handleFn = function() { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } leading = leading || false; var handleFn = function() { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } false; var handleFn = function() { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } ; var handleFn = function() { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } var handleFn = function() { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } var handleFn = function() { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } var handleFn = function() { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } handleFn = function() { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } function() { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } ; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } ; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } var isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } isInvoke = false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } false; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } ; if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } if (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } (!timer) { fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } fn.apply(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } true; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } ; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } // 定时器通过修改timer来修改instant timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } timer = setTimeout(function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } function() { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } { timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } timer = null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } null; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } ; if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } if (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } (!isInvoke) { fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } fn.apply(_this, _arguments); } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } }, delay); } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } else { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } { timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } timer = setTimeout(function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } function() { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } { // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } // 在执行时,通过apply来使用_this和_arguments fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } fn.apply(_this, _arguments); }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } }, delay); } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } // 取消处理 handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } handleFn.cancel = function() { if (timer) clearTimeout(timer); } return handleFn; } function() { if (timer) clearTimeout(timer); } return handleFn; } { if (timer) clearTimeout(timer); } return handleFn; } if (timer) clearTimeout(timer); } return handleFn; } if (timer) clearTimeout(timer); } return handleFn; } if (timer) clearTimeout(timer); } return handleFn; } (timer) clearTimeout(timer); } return handleFn; } } return handleFn; } } return handleFn; } return handleFn; } return handleFn; } return handleFn; } return handleFn; } handleFn; } } } ``` #### 2.3.5. 优化返回值 有时候fn函数执行结束后还有返回值,如果我们希望拿到这个返回值应该怎么办呢? 先明确一个操作: * 内部执行fn函数大多数情况是异步执行的(在setTimeout中执行) * 所以通过return是无法拿到返回值的 异步的操作如何获取返回值呢? * ES6中通过Promise * ES6之前通过回调函数 第一版:Promise的版本: * 这里我给出一版Promise,但是我们希望这个防抖函数尽可能具备通用性 * 所以暂时不建议使用Promise ``` function debounce(fn, delay, leading) { var timer = null; leading = leading || false; var handleFn = function () { return new Promise((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } { var timer = null; leading = leading || false; var handleFn = function () { return new Promise((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var timer = null; leading = leading || false; var handleFn = function () { return new Promise((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var timer = null; leading = leading || false; var handleFn = function () { return new Promise((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var timer = null; leading = leading || false; var handleFn = function () { return new Promise((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } timer = null; leading = leading || false; var handleFn = function () { return new Promise((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } null; leading = leading || false; var handleFn = function () { return new Promise((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } ; leading = leading || false; var handleFn = function () { return new Promise((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } leading = leading || false; var handleFn = function () { return new Promise((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } leading = leading || false; var handleFn = function () { return new Promise((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } false; var handleFn = function () { return new Promise((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } ; var handleFn = function () { return new Promise((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var handleFn = function () { return new Promise((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var handleFn = function () { return new Promise((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var handleFn = function () { return new Promise((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } handleFn = function () { return new Promise((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } function () { return new Promise((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } { return new Promise((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } return new Promise((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } return new Promise((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } return new Promise((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } new Promise((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } new Promise((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } Promise((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } Promise((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } ((resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } (resovle, reject) => { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } ; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } ; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } isInvoke = false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } false; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } ; if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } (!timer) { resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } resovle(fn.apply(_this, _arguments)); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } ; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } timer = setTimeout(function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } function () { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } { timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } timer = null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } null; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } ; if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } (!isInvoke) { resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } resovle(fn.apply(_this, _arguments)); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } function () { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } { // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 在执行时,通过apply来使用_this和_arguments resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } resovle(fn.apply(_this, _arguments)); }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } }, delay); } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } }) } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } function () { if (timer) clearTimeout(timer); } return handleFn; } { if (timer) clearTimeout(timer); } return handleFn; } if (timer) clearTimeout(timer); } return handleFn; } if (timer) clearTimeout(timer); } return handleFn; } if (timer) clearTimeout(timer); } return handleFn; } (timer) clearTimeout(timer); } return handleFn; } } return handleFn; } } return handleFn; } return handleFn; } return handleFn; } return handleFn; } return handleFn; } handleFn; } } } ``` 第二版:回调函数版本 * 因为这一次我们有多个可选参数,所以我们让调用者传入一个option * leading:是否开始的回调直接执行一次 * result:函数类型,通过它来将结果回调出去 ``` function debounce(fn, delay, option) { var timer = null; if (!option) option = {}; leading = option.leading || false; result = option.result || null; var handleFn = function () { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } { var timer = null; if (!option) option = {}; leading = option.leading || false; result = option.result || null; var handleFn = function () { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var timer = null; if (!option) option = {}; leading = option.leading || false; result = option.result || null; var handleFn = function () { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var timer = null; if (!option) option = {}; leading = option.leading || false; result = option.result || null; var handleFn = function () { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var timer = null; if (!option) option = {}; leading = option.leading || false; result = option.result || null; var handleFn = function () { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } timer = null; if (!option) option = {}; leading = option.leading || false; result = option.result || null; var handleFn = function () { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } null; if (!option) option = {}; leading = option.leading || false; result = option.result || null; var handleFn = function () { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } ; if (!option) option = {}; leading = option.leading || false; result = option.result || null; var handleFn = function () { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (!option) option = {}; leading = option.leading || false; result = option.result || null; var handleFn = function () { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (!option) option = {}; leading = option.leading || false; result = option.result || null; var handleFn = function () { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (!option) option = {}; leading = option.leading || false; result = option.result || null; var handleFn = function () { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } (!option) option = {}; leading = option.leading || false; result = option.result || null; var handleFn = function () { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } leading = option.leading || false; result = option.result || null; var handleFn = function () { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } leading = option.leading || false; result = option.result || null; var handleFn = function () { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } false; result = option.result || null; var handleFn = function () { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } ; result = option.result || null; var handleFn = function () { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } result = option.result || null; var handleFn = function () { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } result = option.result || null; var handleFn = function () { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } null; var handleFn = function () { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } ; var handleFn = function () { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var handleFn = function () { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var handleFn = function () { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var handleFn = function () { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } handleFn = function () { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } function () { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } { if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } (timer) clearTimeout(timer); // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 获取this和argument var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } _this = this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } this; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } ; var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } _arguments = arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } arguments; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } ; if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } (leading) { // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 通过一个变量来记录是否立即执行 var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } isInvoke = false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } false; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } ; if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } (!timer) { callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } callFn(_this, _arguments); isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } isInvoke = true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } true; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } ; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 定时器通过修改timer来修改instant timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } timer = setTimeout(function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } function () { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } { timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } timer = null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } null; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } ; if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } (!isInvoke) { callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } callFn(_this, _arguments); } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } }, delay); } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } else { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } { timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } timer = setTimeout(function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } function () { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } { // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 在执行时,通过apply来使用_this和_arguments callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } callFn(_this, _arguments); }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } }, delay); } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } { var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } var res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } res = fn.apply(context, argument); if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } if (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } (result) { result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } result(res); } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } // 取消处理 handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } handleFn.cancel = function () { if (timer) clearTimeout(timer); } return handleFn; } function () { if (timer) clearTimeout(timer); } return handleFn; } { if (timer) clearTimeout(timer); } return handleFn; } if (timer) clearTimeout(timer); } return handleFn; } if (timer) clearTimeout(timer); } return handleFn; } if (timer) clearTimeout(timer); } return handleFn; } (timer) clearTimeout(timer); } return handleFn; } } return handleFn; } } return handleFn; } return handleFn; } return handleFn; } return handleFn; } return handleFn; } handleFn; } } } ``` ## 三. 节流函数的实现 > 因为防抖和节流函数都是对频繁事件的处理,所以我们可以使用相同的案例来演练,另外对应的优化操作也是比较相似的,所以这里不再进行细分,某些代码在实现过程中直接编写。 ### 3.1. 案例和第三方库 我们还是使用之前的案例,在输入框中不断的输入内容,但是回调函数会以固定的频率来执行: ```
``` ### 3.2. 自定义防抖函数 #### 3.2.1. 节流基本功能 节流函数的默认实现思路我们采用时间戳的方式来完成: * 我们使用一个last来记录上一次执行的时间 * 每次准备执行前,获取一下当前的时间now:`now - last > interval` * 那么函数执行,并且将now赋值给last即可 ``` function throttle(fn, interval) { var last = 0; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } { var last = 0; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } var last = 0; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } var last = 0; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } var last = 0; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } last = 0; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } 0; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } ; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } ; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } arguments; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } ; var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } var now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } now = new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } new Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } Date().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } ().getTime(); if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } if (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } (now - last > interval) { fn.apply(_this, _arguments); last = now; } } } fn.apply(_this, _arguments); last = now; } } } fn.apply(_this, _arguments); last = now; } } } last = now; } } } last = now; } } } } } } } } } } } } } } } ``` #### 3.2.2. 优化最后执行 默认情况下,我们的防抖函数最后一次是不会执行的 * 因为没有达到最终的时间,也就是条件 `now - last > interval`满足不了的 * 但是,如果我们希望它最后一次是可以执行的,那么我们可以让其传入对应的参数来控制 我们来看一下代码如何实现: * 我们增加了else语句: * 所以我们可以使用timer变量来记录定时器是否已经开启 * 已经开启的情况下,不需要开启另外一个定时器了 * else语句表示没有立即执行的情况下,就会开启定时器; * 但是定时器不需要频繁的开启,开启一次即可 * 如果固定的频率中执行了回调函数 * 因为刚刚执行过回调函数,所以定时器到时间时不需要执行; * 所以我们需要取消定时器,并且将timer赋值为null,这样的话可以开启下一次定时器; * 如果定时器最后执行了,那么timer需要赋值为null * 因为下一次重新开启时,只有定时器为null,才能进行下一次的定时操作; ``` function throttle(fn, interval) { var last = 0; var timer = null; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } { var last = 0; var timer = null; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var last = 0; var timer = null; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var last = 0; var timer = null; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var last = 0; var timer = null; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } last = 0; var timer = null; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } 0; var timer = null; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } ; var timer = null; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var timer = null; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var timer = null; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var timer = null; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } timer = null; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } null; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } ; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } ; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } ; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } ().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } ; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } ) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } { timer = null; fn.apply(_this, _arguments); }, interval); } } } timer = null; fn.apply(_this, _arguments); }, interval); } } } timer = null; fn.apply(_this, _arguments); }, interval); } } } null; fn.apply(_this, _arguments); }, interval); } } } ; fn.apply(_this, _arguments); }, interval); } } } fn.apply(_this, _arguments); }, interval); } } } fn.apply(_this, _arguments); }, interval); } } } }, interval); } } } }, interval); } } } } } } } } } } } } } } } ``` 我们可以传入一个变量让来确定是否需要最后执行一次: ``` function throttle(fn, interval, option) { var last = 0; var timer = null; if (!option) option = {}; var trailing = option.trailing || false; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } { var last = 0; var timer = null; if (!option) option = {}; var trailing = option.trailing || false; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var last = 0; var timer = null; if (!option) option = {}; var trailing = option.trailing || false; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var last = 0; var timer = null; if (!option) option = {}; var trailing = option.trailing || false; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var last = 0; var timer = null; if (!option) option = {}; var trailing = option.trailing || false; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } last = 0; var timer = null; if (!option) option = {}; var trailing = option.trailing || false; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } 0; var timer = null; if (!option) option = {}; var trailing = option.trailing || false; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } ; var timer = null; if (!option) option = {}; var trailing = option.trailing || false; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var timer = null; if (!option) option = {}; var trailing = option.trailing || false; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var timer = null; if (!option) option = {}; var trailing = option.trailing || false; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var timer = null; if (!option) option = {}; var trailing = option.trailing || false; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } timer = null; if (!option) option = {}; var trailing = option.trailing || false; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } null; if (!option) option = {}; var trailing = option.trailing || false; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } ; if (!option) option = {}; var trailing = option.trailing || false; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } if (!option) option = {}; var trailing = option.trailing || false; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } if (!option) option = {}; var trailing = option.trailing || false; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } if (!option) option = {}; var trailing = option.trailing || false; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } (!option) option = {}; var trailing = option.trailing || false; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var trailing = option.trailing || false; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var trailing = option.trailing || false; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var trailing = option.trailing || false; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var trailing = option.trailing || false; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } trailing = option.trailing || false; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } false; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } ; return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } return function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } ; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } ; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } ().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } null; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } ; } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } fn.apply(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } function() { timer = null; fn.apply(_this, _arguments); }, interval); } } } { timer = null; fn.apply(_this, _arguments); }, interval); } } } timer = null; fn.apply(_this, _arguments); }, interval); } } } timer = null; fn.apply(_this, _arguments); }, interval); } } } null; fn.apply(_this, _arguments); }, interval); } } } ; fn.apply(_this, _arguments); }, interval); } } } fn.apply(_this, _arguments); }, interval); } } } fn.apply(_this, _arguments); }, interval); } } } }, interval); } } } }, interval); } } } } } } } } } } } } } } } ``` #### 3.2.3. 优化取消功能 取消功能和防抖函数类似: ``` function throttle(fn, interval) { var last = 0; var timer = null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } { var last = 0; var timer = null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } var last = 0; var timer = null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } var last = 0; var timer = null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } var last = 0; var timer = null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } last = 0; var timer = null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } 0; var timer = null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } ; var timer = null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } var timer = null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } var timer = null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } var timer = null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } timer = null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } ; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } ; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } ; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } ().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } if (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } (timer) { clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } clearTimeout(timer); timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } timer = null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } null; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } ; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } fn.apply(_this, _arguments); last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } last = now; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } else if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } if (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } (timer === null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } null) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } ) { // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } // 只是最后一次 timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } timer = setTimeout(function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } function() { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } { timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } timer = null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } null; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } ; fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } fn.apply(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } handleFn.cancel = function() { clearTimeout(timer); timer = null; } return handleFn; } function() { clearTimeout(timer); timer = null; } return handleFn; } { clearTimeout(timer); timer = null; } return handleFn; } clearTimeout(timer); timer = null; } return handleFn; } clearTimeout(timer); timer = null; } return handleFn; } timer = null; } return handleFn; } timer = null; } return handleFn; } null; } return handleFn; } ; } return handleFn; } } return handleFn; } } return handleFn; } return handleFn; } return handleFn; } return handleFn; } return handleFn; } handleFn; } } } ``` #### 3.2.4. 优化返回值 和防抖函数类似,使用Promise或者回调函数,这里我还是采用回调函数: ``` function throttle(fn, interval, option) { var last = 0; var timer = null; if (!option) option = {}; var trailing = option.trailing || false; var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }{ var last = 0; var timer = null; if (!option) option = {}; var trailing = option.trailing || false; var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } var last = 0; var timer = null; if (!option) option = {}; var trailing = option.trailing || false; var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } var last = 0; var timer = null; if (!option) option = {}; var trailing = option.trailing || false; var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }var last = 0; var timer = null; if (!option) option = {}; var trailing = option.trailing || false; var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } last = 0; var timer = null; if (!option) option = {}; var trailing = option.trailing || false; var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }0; var timer = null; if (!option) option = {}; var trailing = option.trailing || false; var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }; var timer = null; if (!option) option = {}; var trailing = option.trailing || false; var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } var timer = null; if (!option) option = {}; var trailing = option.trailing || false; var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } var timer = null; if (!option) option = {}; var trailing = option.trailing || false; var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }var timer = null; if (!option) option = {}; var trailing = option.trailing || false; var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } timer = null; if (!option) option = {}; var trailing = option.trailing || false; var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }null; if (!option) option = {}; var trailing = option.trailing || false; var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }; if (!option) option = {}; var trailing = option.trailing || false; var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } if (!option) option = {}; var trailing = option.trailing || false; var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } if (!option) option = {}; var trailing = option.trailing || false; var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }if (!option) option = {}; var trailing = option.trailing || false; var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } (!option) option = {}; var trailing = option.trailing || false; var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } var trailing = option.trailing || false; var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } var trailing = option.trailing || false; var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } var trailing = option.trailing || false; var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }var trailing = option.trailing || false; var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } trailing = option.trailing || false; var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }false; var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }; var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }var result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } result = option.result || null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }null; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }; var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }var handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } handleFn = function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }function() { // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }{ // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } // this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }// this和argument var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }var _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } _this = this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }this; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }; var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }var _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } _arguments = arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }arguments; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }; var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }var now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } now = new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }new Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }Date().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }().getTime(); if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }if (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } (now - last > interval) { if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }if (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } (timer) { clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } clearTimeout(timer); timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } timer = null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }null; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } callFn(_this, _arguments); last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } last = now; } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } } else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }else if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }if (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } (timer === null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }null && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } && trailing) { // 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }// 只是最后一次 timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } timer = setTimeout(function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }function() { timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }{ timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } timer = null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }null; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }; callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } callFn(_this, _arguments); }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } }, interval); } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } handleFn.cancel = function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }function() { clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }{ clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } clearTimeout(timer); timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } timer = null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }null; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }function callFn(context, argument) { var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }{ var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; }var res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } res = fn.apply(context, argument); if (result) { result(res); } } return handleFn; } if (result) { result(res); } } return handleFn; } if (result) { result(res); } } return handleFn; }if (result) { result(res); } } return handleFn; } (result) { result(res); } } return handleFn; } result(res); } } return handleFn; } result(res); } } return handleFn; } } } return handleFn; } } } return handleFn; } } return handleFn; } } return handleFn; } return handleFn; } return handleFn; } return handleFn; }return handleFn; } handleFn; } }} ```