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/
- Follow-Ups:
- [Javascript] Avoid multiple onclick
- From: John Deighan
- [Javascript] Avoid multiple onclick
- References:
- [Javascript] Avoid multiple onclick
- From: Guillaume
- [Javascript] Avoid multiple onclick
- From: Paul Novitski
- [Javascript] Avoid multiple onclick
- From: Bill Moseley
- [Javascript] Avoid multiple onclick
- Prev by Date: [Javascript] Avoid multiple onclick
- Next by Date: [Javascript] Avoid multiple onclick
- Previous by thread: [Javascript] Avoid multiple onclick
- Next by thread: [Javascript] Avoid multiple onclick
- Index(es):