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: nick at nickfitz.co.uk (Nick Fitzsimons)
  • Subject: [Javascript] Avoid multiple onclick
  • Date: Wed Oct 18 11:39:48 2006

On 18 Oct 2006, at 14:47, Bill Moseley wrote:

> On Wed, Oct 18, 2006 at 02:23:16AM -0700, Paul Novitski wrote:
>> One method of preventing a function from running more than once is to
>> set a global variable the first time the function is called, and
>> refuse to run if that variable is set:
>>
>>         var bInitialized = false;
>>         ...
>>         function DoSomething()
>>         {
>>                         if (bInitialized) return;
>>                 bInitialized = true;
>>
>>                 ...
>>         }
>
> Is there a way to do that atomically?
>

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.

Cheers,

Nick.
-- 
Nick Fitzsimons
http://www.nickfitz.co.uk/