Redial Timeout () with 1 milisec

Here is a snippet of code from a 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(); } 

This is a hack when the DOM is ready for IE. Although it seems very beautiful in setTimeout( doScrollCheck, 1 ); bothers me a bit setTimeout( doScrollCheck, 1 ); , which means that the doScrollCheck() function is called 1000 times per second until the DOM is ready.

Should I expect this to be a huge performance leak?

+6
source share
3 answers

The setTimeout function is almost never called at exactly the specified time. The browser is free to do +/- in time for several milliseconds, at least, and if there is any other intensive work, then it can be delayed for several seconds or more. As already mentioned, the browser also implements minimal latency. This 1 millisecond simply tells the browser, "as soon as you finish doing everything you do, let me know so that I can do something." It also gives execution from Javascript so that the browser can return to doing what it does next.

+3
source

I donโ€™t think that it is until the DOM is ready, the user cannot iterate with the page anyway, and the verification of all-1ms stops as soon as the DOM is prepared.

Also, this doScrollCheck function is not the same as setInterval (doScrollCheck, 1). The latter did not wait for doScrollCheck to complete, it would just run the doScrollCheck function as fast as possible. This can be a performance issue because there can be dozens of overlapping calls to the same function.

I'm not sure if I saw any other way to find that the DOM is ready, that by checking something (in this case the property is available on documentElement) for longer / shorter intervals.

0
source

The minimum timeout interval is significantly greater than 1 millisecond. If your code had a lot of manipulations with the DOM, then even when the pause was forced for a longer period, you would start to heat up the processor.

Some newer browsers (Firefox and Chrome, possibly Safari) increase this minimum timeout much more for unfocused tabs and windows.

Even a millisecond is a long time for a modern processor.

0
source

Source: https://habr.com/ru/post/895744/


All Articles