Show a javascript loader image while function is executing
-
I want to click a button, show a loader image, execute a time-consuming function, hide the loader image. When I attempt this, the page freezes until the entire event runs, so the loader image is never seen. Here is a sample: $('#btn').click(function() { $('#LoadingImage').show(); <time consuming function> $('#LoadingImage').hide(); }); How can I distinctly show the loader image, update the screen, run the function, hide the loader image?
-
Answer:
Although what you have should work, try doing this: $('#btn').click(function() { $('#LoadingImage').show(); setTimeout(function() { <time consuming function> $('#LoadingImage').hide(); }, 0); }); This will http://blog.thinkature.com/index.php/2006/11/26/escaping-the-javascript-call-stack-with-settimeout/. If THAT doesn't work, then post more details as to what this mystery time consuming function is.
David at Stack Overflow Visit the source
Other answers
For various reasons JS has to execute on the main thread, which means that painting is blocked as long as JS is executing -- even multiprocess architectures like chrome cannot repaint a tab if that tab is executing JS. Your only way to get painting to occur will be to split up execution of your function, or move the function onto an html5 worker thread -- and they only work on the betas of Firefox and Safari (or more generally any sufficiently recent build of webkit proper)
olliej
Related Q & A:
- How to place a different background image on each part of a book?Best solution by TeX - LaTeX
- How to properly end a javascript statement?Best solution by w3schools.com
- How to pass a javascript object back to the server?Best solution by developer.mozilla.org
- Will there be a difference in image resolution between my home printer and a big print place?Best solution by Yahoo! Answers
- Need help debugging a javascript?Best solution by allwebdevhelp.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.