overriding a global function in javascript

后端 未结 2 2417
温柔的废话
温柔的废话 2021-02-20 16:37

I am trying to add my own error handling to the JavaScript setTimeout function. The following code works fine in chrome:

var oldSetTimeout = window.setTimeout;
         


        
2条回答
  •  北恋
    北恋 (楼主)
    2021-02-20 16:59

    Minor improvement to the Answer of Tim Down to mimic the original even more:

    window.oldSetTimeout = window.setTimeout;
    window.setTimeout = function(func, delay) {
        return window.oldSetTimeout(function() {
            try {
                func();
            }
            catch (exception) {
                //Do Error Handling
            }
        }, delay);
    };
    

提交回复
热议问题