开发者

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).

The code will be along these lines:

var nativeSetTimeout = window.setTimeout;
window.setTimeout = function(fn, i开发者_开发技巧nterval){
    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?


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. 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, E.g. function setSafeTimeout(fn, timeout) { ... ; return setTimeout(fn, timeout);}.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜