Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

repeated setTimeout() with 1 milisec

Tags:

jquery

Here is a piece of code from the jQuery source (bit.ly/jqsource):

// The DOM ready check for Internet Explorer
function doScrollCheck() {
    if ( jQuery.isReady ) {
        return;
    }

    try {
        // If IE is used, use the trick by Diego Perini
        // http://javascript.nwbox.com/IEContentLoaded/
        document.documentElement.doScroll("left");
    } catch(e) {
        setTimeout( doScrollCheck, 1 );
        return;
    }

    // and execute any waiting functions
    jQuery.ready();
}

It is a hack to detect when the DOM is ready for IE. While theoretically this seems very beautiful, I'm a little concerned about setTimeout( doScrollCheck, 1 );, which means that the function doScrollCheck() is called 1000 times per sec before the DOM is ready.

Should I expect this to be a huge performance drain?

like image 233
Randomblue Avatar asked Aug 24 '11 13:08

Randomblue


1 Answers

The setTimeout function is almost never called at exactly the requested time. The browser is free to do a +/- on the time by a few milliseconds, at the least, and if there is any other intensive work going on then it can be delayed by seconds or more. As others have already mentioned, the browser also implements a minimum timeout time. This 1 millisecond simply tells the browser 'as soon as you're done doing whatever you're doing, let me know so I can do something'. It also yields execution from the Javascript so that the browser can return to doing whatever it will do next.

like image 196
Zimzat Avatar answered Oct 23 '22 05:10

Zimzat