Links

Lists

Latest Updates

Ruby On Rails List
Python list
Advanced Java
The JavaScript List
Apache Users
Full Disclosure
Linux Security

Search the archives!


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Javascript] Avoid multiple onclick


  • From: blmatthews at gmail.com (Brian L. Matthews)
  • Subject: [Javascript] Avoid multiple onclick
  • Date: Thu Oct 19 12:39:34 2006

>At 12:13 PM 10/18/2006, Nick Fitzsimons wrote:
>All JS code runs in a single thread, so you don't need to worry about
>>doing it atomically. That is, it's not possible for any other
>>JavaScript code to run until the DoSomething function has run to its
>>conclusion. So if DoSomething was called twice, there would be no
>>risk of the "if" line executing, and then another instance of the
>>function running, making the same check and seing the not-yet-true
>>value.
>
>That (i.e. the fact that JS runs in a single thread) answers one 
>question I have, and leads to another. I have a progress bar that's 
>animated via setInterval(). Then, I run a function to load data that 
>can run for quite some time. During that time, the progress bar 
>won't run - I assume due to the fact that my load data function is 
>hogging the CPU. I'd like to arrange for the load data function to 
>give up sufficient CPU to allow the progress bar to run. However, I 
>can't find a sleep() or similar function. Is there a way for my load 
>data function to give up sufficient CPU for the progress bar 
>animation to run?

Yes, you have to split it into chunks and run the chunks from 
setTimeout (or setInterval, but I prefer setTimeout as it gives you 
more control over when things run). You can do something like this:

function doIt()
{
	...run until done or some time has passed...

	if (!done)
		setTimeout("doIt()", 100);
}

By returning from doIt, you allow the browser to run any other 
setTimeout/setInterval expressions. After a bit, doIt is run again so 
you can do some more processing.

Another alternative is to call the progress bar update from doIt 
directly. However, you still need to give up time to the browser by 
returning from doIt, otherwise the browser won't update the page so 
it will look like the progress bar isn't advancing.

Brian