Force Dom Redraw With Javascript On Demand
Solution 1:
Use JQuery show
and hide
callbacks (or other way to display something like fadeIn/fadeOut).
$(document).ready(function () {
$('#enlace').click(function () {
var kitty = $('#kitty');
// see: http://unixpapa.com/js/sleep.htmlfunctionsleepStupidly(usec) {
var endtime = newDate().getTime() + usec;
while (newDate().getTime() < endtime);
}
kitty.show(function () {
// simulates bussy proccess, calling some function...sleepStupidly(4000);
// when this triggers the img style do refresh!// but not beforealert('now you do see it');
kitty.hide();
});
});
});
Solution 2:
Use window.setTimeout()
with some short unnoticeable delay to run slow function:
$(document).ready(function() {
$('#enlace').click(function() {
showImage();
window.setTimeout(function() {
sleepStupidly(4000);
alert('now you do see it');
hideImage();
}, 50);
});
});
Solution 3:
To force redraw, you can use offsetHeight or getComputedStyle().
var foo = window.getComputedStyle(el, null);
or
var bar = el.offsetHeight;
"el" being a DOM element
Solution 4:
I do not know if this works in your case (as I have not tested it), but when manipulating CSS with JavaScript/jQuery it is sometimes necessary to force redrawing of a specific element to make changes take effect.
This is done by simply requesting a CSS property.
In your case, I would try putting a kitty.position().left;
before the function call prior to messing with setTimeout
.
Solution 5:
What worked for me is setting the following:
$(element).css('display','none');
After that you can do whatever you want, and eventually you want to do:
$(element).css('display','block');
Post a Comment for "Force Dom Redraw With Javascript On Demand"