Last active
August 29, 2015 14:02
-
-
Save jixunmoe/adde6f41a58ea47f3c94 to your computer and use it in GitHub Desktop.
Cancel Timeout
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 清空之前的 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); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
setTimeout不止两个参数的,所以用
oldSetTimeout.apply(this, arguments)
好点。