简体   繁体   English

更换 window.setTimeout

[英]Replacing window.setTimeout

Guys, I'm thinking of replacing window.setTimeout with a function of my own (for example we could have a generic error handler for exceptions thrown in callbacks).伙计们,我正在考虑用我自己的 function 替换 window.setTimeout(例如,我们可以有一个通用的错误处理程序来处理回调中抛出的异常)。

The code will be along these lines:代码将遵循以下几行:

var nativeSetTimeout = window.setTimeout;
window.setTimeout = function(fn, interval){
    nativeSetTimeout(function(){
            try{
               fn();
            }catch(e){
                // handle errors coming from the execution of fn
            }
        }, interval);
}

Given that my app will stay in a single iframe, is there any outstanding issue that I should be concerned about?鉴于我的应用程序将保留在单个 iframe 中,是否有任何未解决的问题需要我关注?

I wouldn't do that because it's true it can help in your own development, but replacing standard functions could break third-party libraries.我不会这样做,因为它确实可以帮助您自己的开发,但是替换标准函数可能会破坏第三方库。

Basically, using your own function without replacing standard one should be the way to go.基本上,使用您自己的 function 而不更换标准的应该是 go 的方式。 Use it in your code.在您的代码中使用它。

You could do it that way, but unless you're deliberately trying to intercept the behaviour of existing code or third party libraries, it would be clearer to just write a wrapper with an alternative name, Eg function setSafeTimeout(fn, timeout) {... ; return setTimeout(fn, timeout);}可以这样做,但除非你故意试图拦截现有代码或第三方库的行为,否则只写一个具有替代名称的包装器会更清楚,例如function setSafeTimeout(fn, timeout) {... ; return setTimeout(fn, timeout);} function setSafeTimeout(fn, timeout) {... ; return setTimeout(fn, timeout);} . function setSafeTimeout(fn, timeout) {... ; return setTimeout(fn, timeout);}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM