[Javascript] Order of script-processing

Peter-Paul Koch pp.koch at gmail.com
Sun Sep 25 06:59:40 CDT 2005


> I seem to remember that in the past we have talked about the order that JS
> actually processes its commands, and the results are not always what you
> might expect.
>
> I've just come across that now, and can't work out a way to fix it. I have a
> very slow routine that takes about 5 seconds to process even on a P4 2.4Ghz
> machine. My idea was to have a 'loading - please wait' gif who's visibility
> is initially set to 'hidden'. At the crucial point, I would make the gif
> visible, do the routine, then make the gif 'hidden' once more.
>
> However, when I initiate this process by clicking on a button, the gif
> doesn't display at all. Hmm - why would that be? Removing the 'make the gif
> hidden once again' code reveals that when the button is clicked, the gif is
> not made visible until the slow routine has completed, which means there's
> no point in doing it! This occurs even though the code is
>
> show_the_gif();
> slow_routine();
> hide_the_gif();
>
>
> Any thoughts ?

Most browsers (except Opera) change the visible display of the page
only when all scripts have exited completely. Solution: use a timeout:

function show()
{
  gif.src = 'pleasewait.gif'; // or whatever
  setTimeout('slow_routine()',10);
}

Now the script exits after the gif has been set and the browsers show
it. Then the slow routine kicks in. Result: user sees Please Wait.

--
-------------------------------------------------------------------
ppk, freelance web developer
http://www.quirksmode.org/
------------------------------------------------------------------



More information about the Javascript mailing list