I have a long task in Javascript that should be performed before the web page content is displayed. During the execution of this task I would like to show an image whose opacity will grow up to 100% (when the task is done).
How this can be achieved ?
I have a long task in Javascript that should be performed before the web
Share
In your function , say it’s 10 loops, every loop you need to increase the opacity, like this:
You can see a working demo here
On the final step this would be setting the opacity to
1, completely faded in. The steps you just need to divide out, you need to increase opacity by1/numOfStepseach step…a.fadeIn()won’t work here because it’ll just execute after your code is done…you need to manually set the opacity using.css()inside the loop.The
setTimeout()(without an argument it’s instant) lets the UI update before the next step starts.Note: this works cross-browser, just use
opacityand it’ll take care of settingfilter:in IE for instance.