Skip to content

Instantly share code, notes, and snippets.

@JixunMoe JixunMoe/clearTimeout.js
Last active Aug 29, 2015

Embed
What would you like to do?
Cancel Timeout
// 清空之前的 timeout, 如果尚未执行
(function () {
for (var i = setTimeout(function () {}, 0); --i; )
clearTimeout (i);
});
// 添加到脚本声明可「强行启用旧版沙盒」模式,感谢 @坐怀则乱 指正。
// @grant unsafeWindow
// 备份原始函数
var oldSetTimeout = unsafeWindow.setTimeout;
unsafeWindow.setTimeout = function (foo) {
// 检测函数
if (!foo || foo.toString().indexOf ('关键字') != -1)
return 0;
// 调用原始函数 (感谢 @tiansh 指正)
return oldSetTimeout.apply(unsafeWindow, arguments);
};
@tiansh

This comment has been minimized.

Copy link

tiansh commented Jun 13, 2014

setTimeout不止两个参数的,所以用 oldSetTimeout.apply(this, arguments) 好点。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.