I believe the essence of this question, based on your comments, is that you want to be sure the browser has time to update the screen before it starts a long-running synchronous task. The browser updates the screen when it can, so if you block the main thread, you can't get a refreshed UI. This should be possible with a simple timeout, or as comments have suggested, requestAnimationFrame
.
Simple Timeout:
// For example, document.write(...)
funcThatChangesScreen();
setTimeout(function () {
// For example, lots of heavy calculations
synchronousFuncThatTakesALongTime();
}, 100);
JSFiddle comparison:
Or, see requestAnimationFrame.
Edit: As others in the comments have noted, this is not truly the best way to get what you want, even if it does work. If you have a long-running synchronous task, it may be best to delegate it to a WebWorker or make it asynchronous. This timeout solution will allow the user to see the text on the screen, but it will still block the UI after, like in the JSFiddles above.