title: overriding a global function in javascript
body: <p>I am trying to add my own error handling to the JavaScript setTimeout function. The following code works fine in chrome: </p>
  
  <pre><code>var oldSetTimeout = window.setTimeout;
  window.setTimeout = function setTimeout(func, delay) {
      var args = Array.prototype.slice.call(arguments, 0);
      args[0] = function timeoutFunction() {
          var timeoutArgs = Array.prototype.slice.call(arguments, 0);
          try {
              func.apply(this,timeoutArgs);
          }
          catch (exception) {
              //Do Error Handling
          }
      }
      return oldSetTimeout.apply(this, args);
  }
  </code></pre>
  
  <p>But in IE7 it turns into a recursive function. For some reason <code>oldSetTimeout</code> gets set to the new function. </p>
  
  <p>Any suggestions?  </p>
  
  <p><br></p>
  
  <p><hr>
  side note: Yes, I need to do it this way. I am using a pile of 3rd party libraries all of which don't deal with setTimeout well, so I can't just change the calls to setTimeout.</p>