[Javascript] Avoid multiple onclick
Nick Fitzsimons
nick at nickfitz.co.uk
Wed Oct 18 11:13:43 CDT 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/
More information about the Javascript
mailing list