4 ms·
That does not appear to be the case in Firefox (62.0). Using dandare's blue button adding function from his reply to your comment here is some test code: f
by tzs 8y ago
That does not appear to be the case in Firefox (62.0). Using dandare's blue button adding function from his reply to your comment here is some test code:
function myFunction()
{
var btn = document.createElement("BUTTON");
var t = document.createTextNode("CLICK ME");
btn.appendChild(t);
document.body.appendChild(btn);
btn.style.color = "blue";
}
function slow(n)
{
console.log('slow called');
let s = 0;
for (let i = 0; i < n; ++i)
s += 1.00000001;
console.log('slow:' + s);
return s;
}
function callMyFunction(to)
{
myFunction();
setTimeout(function () {slow(5000000000);}, to);
}
The argument to slow() in the timeout function, 5000000000, causes it to take about 5 seconds on my system, long enough to easily tell if the button has rendered before slow runs.
In Chrome, callMyFunction(0) results in the blue button showing up, then 'slow called' in the log, then 5 seconds later slow() finishes. I did this several times and the results were the same every time.
In Firefox, it results in 'slow called' in the log, then 5 seconds later slow finishes and the blue button shows up. Several tries with callMyFunction(0) resulted in no instances of the rendering being done before the timeout function was called.
I had to bump the argument to callMyFunction up to 35 to get the rendering to happen consistently before the timeout function (10 out of 10 times). It mostly completed before the timeout at 34, but not always.
This is surprising to me, because the impression I got when I looked into this a few years ago on Stackoverflow and other web sites was the setTimeout(0) was a reliable way to make sure that all DOM changes were rendered.
My test procedure: open browser. Open inspector. Paste the above code into the console. invoke callMyFunction from the console while watching the screen for the button and the log for slow's messages. I wonder if JavaScript invoked from the console might have some different rules for rendering timing?
Edit: nope, it has nothing to do with the console. I tried the above, but invoked by a click on a page instead of from the console, and the results are the same.