Skip to content

Instantly share code, notes, and snippets.

@jixunmoe
Last active August 29, 2015 14:02
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save jixunmoe/adde6f41a58ea47f3c94 to your computer and use it in GitHub Desktop.
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
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