the Future of the Web
  • Articles
  • Contact
  • Detecting and Debugging Timeouts and Intervals

    May 28 2007

    When you start to cram a lot of JavaScript animations and Ajax onto a web page, it can become tricky to know all the code that's running in the background. When you start to detect some performance issues, it's equally tricky to track down what code is being executed.

    Luckily, the only way to get code to run in the background with JavaScript is through the functions setTimeout and setInterval. And more luckily, we can overwrite these functions so that we can know whenever they are being called:

    window.setInterval_old = window.setInterval;
    window.setInterval = function(fn, time){
        console.log('interval', fn.toString(), time);
    
        return window.setInterval_old(function(){
            console.log('interval executed', fn.toString());
            fn();
        }, time);
    };
    
    window.setTimeout_old = window.setTimeout;
    window.setTimeout = function(fn, time){
        console.log('timeout', fn.toString(), time);
    
        return window.setTimeout_old(function(){
            console.log('timeout executed', fn.toString());
            fn();
        }, time);
    };
    

    This will send output to Firebug whenever a timeout or interval is first initiated, and again when the function is actually called.

    This solution is rather nice because it will let you know what 3rd party JavaScript widgets are doing in the background as well without needing to add debugging messages to them. You could overwrite nearly any method like this to get similar debugging messages as well (like document.getElementById or Array.prototype.push or practically anything).

    View 5 Comments | Add a comment
  • Comments

    1. hp at 4:08am on August 21, 2007

    very  good!!!

    2. Jim at 4:46pm on April 16, 2008

    Thanks, a lot. But it doesn't work for me.

    "fn is not a function"

    :(

    Please, help me.

    JIM

    3. Jesse Skinner at 4:59pm on April 16, 2008

    @Jim, does it work when you run these?

    setTimeout(function(){ console.log('setTimeout()'); }, 100);
    setInterval(function(){ console.log('setInterval()'); }, 1000);

    If not, could you paste your code?

    4. Jim at 5:04pm on April 16, 2008

    It works fine.

    My code is the problem.

    I'll send a mail with the code and more details.

    Thanks.


    JIM

    5. Yohann.M at 12:39pm on November 15, 2010

    This didn't work for me either. Here my improvments:

    window.setTimeout_old = window.setTimeout;
    window.setTimeout = function(fn, time){
      console.log('setTimeout for "' + fn.toString() + '" ' + (time/1000) + "s");
      return window.setTimeout_old(function(){
          console.log('setTimeout ended - Executing ' + ' ' + fn.toString());
          eval(fn);
      }, time);
    };

    Add a Comment

    Note: HTML tags and entities will be converted so that they are displayed as you type them. This means if you type in <em>, people will see <em>, and if you type &lt;em&gt;, people will see &lt;em&gt;.

  • Jesse Skinner

    Jesse Skinner
    • About Me
    • Email Me
    • RSS Feed RSS Icon
    • @JesseSkinner
  • Recent Articles

    • Free eBook: Unobtrusive Ajax
    • Official jQuery Templating Plugin
    • jQuery Live Events
    • buttons need type="submit" to submit in IE
    • Use Arrays in HTML Form Variables
    • 5 Reasons Freelancers Can Succeed in a Shrinking Economy
    • Keeping a Live Eye on Logs
    • Using PHP's empty() Instead of isset() and count()
    • Testing Web Pages with Lynx
    • Stop CSS Background Flickering in Internet Explorer 6
    • See All...
  • Categories

    • javascript (41)
    • links (17)
    • about (17)
    • web (14)
    • html (12)
    • server (11)
    • css (8)
    • browsers (8)
    • carnival (7)
    • work (5)
    • design (4)
    • seo (4)
    • ads (4)
    • events (4)
    • standards (4)
  • Older Articles

    • October 2010
    • February 2009
    • January 2009
    • December 2008
    • November 2008
    • October 2008
    • July 2008
    • June 2008
    • May 2008
    • April 2008
    • February 2008
    • January 2008
    • December 2007
    • November 2007
    • September 2007
    • August 2007
    • July 2007
    • June 2007
    • May 2007
    • April 2007
    • March 2007
    • February 2007
    • January 2007
    • December 2006
    • November 2006
    • October 2006
    • September 2006
    • August 2006
    • July 2006
    • June 2006
    • May 2006
    • April 2006
    • March 2006
    • February 2006
    • January 2006
    • December 2005
    • November 2005
    • October 2005
    • September 2005
    • August 2005
    • April 2005
    • See All...
Copyright © 2012 The Future of the Web