[Javascript] Avoid multiple onclick

John Warner john at jwarner.com
Tue Nov 7 16:58:40 CST 2006


It was creating a new variable inside the function (scope).
 
 
John Warner

-----Original Message-----
From: javascript-bounces at LaTech.edu
[mailto:javascript-bounces at LaTech.edu] On Behalf Of Guillaume
Sent: Tuesday, November 07, 2006 5:56 PM
To: [JavaScript List]
Subject: Re: [Javascript] Avoid multiple onclick


John,

Yep, so simple and working... Thanks !!
I was trying the same but with:

var bInitialized = false; 
Wich strangely was not working.
Regards.
Guillaume.


 function DoSomethingElse()

  

        {

                       // Need to re-enable the function 

above to run again



                ...

    

bInitialized = false;    // <<<-------------

  

        }



    



John Warner





  

-----Original Message-----

From: javascript-bounces at LaTech.edu 

[mailto:javascript-bounces at LaTech.edu] On Behalf Of Guillaume

Sent: Tuesday, November 07, 2006 2:07 PM

To: [JavaScript List]

Subject: Re: [Javascript] Avoid multiple onclick





Paul,



This is perfectly working... This function will work once 

only... However how do I allow that function to run again 

once another function 

has been called ?



var bInitialized = false;

        ...

        function DoSomething()

        {

                        if (bInitialized) return;

                bInitialized = true;



                ...

        }





        function DoSomethingElse()

        {

                       // Need to re-enable the function 

above to run again



                ...

        }





    

At 10/18/2006 01:59 AM, Guillaume wrote:



      

So what I'd like is the functions to fire only once even if onclick

is performed multiple times.

        



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;



                ...

        }



Regards,

Paul

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20061107/bf54ef18/attachment.htm>


More information about the Javascript mailing list