javigzz javigzz - 1 year ago 64
Javascript Question

force DOM redraw with javascript on demand

The title of the question expresses what I think is the ultimate question behind my particular case.

My case:
Inside a click handler, I want to make an image visible (a 'loading' animation) right before a busy function starts. Then I want to make it invisible again after the function has completed.
Instead of what I expected I realize that the image never becomes visible. I guess that this is due to the browser waiting for the handler to end, before it can do any redrawing (I am sure there are good performance reassons for that).

The code (also in this fiddle: http://jsfiddle.net/JLmh4/2/)


<img id="kitty" src="http://placekitten.com/50/50" style="display:none">
<div><a href="#" id="enlace">click to see the cat</a> </div>


var kitty = $('#kitty');

// see: http://unixpapa.com/js/sleep.html
function sleepStupidly(usec)
var endtime= new Date().getTime() + usec;
while (new Date().getTime() < endtime)

// simulates bussy proccess, calling some function...


// when this triggers the img style do refresh!
// but not before
alert('now you do see it');


I have added the
call right after the
function to show that in that moment of rest, the browser does redraw, but not before. I innocently expected it to redraw right after setting the 'display' to 'block';

For the record, I have also tried appending html tags, or swapping css classes, instead of the image showing and hiding in this code. Same result.

After all my research I think that what I would need is the ability to force the browser to redraw and stop every other thing until then.

Is it possible? Is it possible in a crossbrowser way? Some plugin I wasn't able to find maybe...?

I thought that maybe something like 'jquery css callback' (as in this question: In JQuery, Is it possible to get callback function after setting new css rule?) would do the trick ... but that doesn't exist.

I have also tried to separte the showing, function call and hiding in different handlers for the same event ... but nothing. Also adding a
to delay the execution of the function (as recommended here: Force DOM refresh in JavaScript).

Thanks and hope it also helps others.


EDIT (after setting my preferred answer):

Just to further explain why I selected the window.setTimeout strategy.
In my real use case I have realized that in order to give the browser time enough to redraw the page, I had to give it about 1000 miliseconds (much more than the 50 for the fiddle example). This I belive is due to a deeper DOM tree (in fact, unnecessary deep).
The setTimeout let approach lets you do that.

Answer Source

Use window.setTimeout() with some short unnoticeable delay to run slow function:

$(document).ready(function() {
    $('#enlace').click(function() {

        window.setTimeout(function() {
            alert('now you do see it');
        }, 50);

Live demo