4 Replies Latest reply on Sep 20, 2011 11:34 PM by pramod.rao

    How can I ignore multiple clicks while processing the first one?

    JonInAms

      Hi -

       

      I am writing my first Flex program so I'm sorry if this is a newbie question.

       

      I have some code which handles a click but takes a while to finish. What I want to do is proces steh first click but ignore any more clicks until the code has finished dealing with the first one.

       

      Can anyone tell me the standard way to do this?

       

      Here's a simplified example with a builtin delay. If I click on the button several times and then wait, the clicks are all processed in sequence and the test count slowly increases. What I want the code to do is ignore any clicks which teh user made during the two second delay but start handling new clicks after the delay has finished.

       

      Example Code...

       

      <<?xml version="1.0"?>
      <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
            xmlns:s="library://ns.adobe.com/flex/spark"
            xmlns:mx="library://ns.adobe.com/flex/mx">
         <fx:Script>
            <![CDATA[
      
      
      private var count:Number = 0;
      
      
      private function clickHandler():void {
      
      
         // Something which takes a while to complete...
         var timer:Date = new Date();
         while( (new Date()).valueOf() - timer.valueOf() <  2000 ) {
            var nop:int = 0;
         }
      
      
         // Report result
         count++;
         output.text="Test Number "+count;   
      }
      
      
      ]]>
         </fx:Script>
      
      
         <s:Panel title="Example">
            <s:VGroup left="10" right="10" top="10" bottom="10">
                 <s:Label id="output" text=""/>
                 <s:Button label="Click Me" click="clickHandler();"/>
            </s:VGroup>
         </s:Panel>
      
      
      </s:Application>
      

       

      Thanks

       

      - Jon

        • 1. Re: How can I ignore multiple clicks while processing the first one?
          JonInAms Level 1

          HI -

           

          I found a work around, but I'm sure its not the best way...

          private var prevDate:Date = new Date();
          
          private function clickHandler():void {
          
             // Ensure user waits before clicking again
             var thisDate:Date = new Date();
             if ( thisDate.valueOf() - prevDate.valueOf() > 100 ) {
          
                  // stuff that takes a while goes here
          
                  prevDate = new Date();
             }
          }
          

           

          ...so just before the handler exists it sets prevDate, and it ignores any more clicks until 0.1 of a second later

           

          I also tried giving the button an id of mybutton and starting and stopping the function with "mybutton.enabled=false" and "mybutton.enabled=true" but it didn't seem to have any effect

           

          Then I tried stopping and starting with "mybutton.removeEventListener(MouseEvent.CLICK, clickHandler)" and    "mybutton.addEventListener(MouseEvent.CLICK, clickHandler)" but got an "Argument count mismatch" error

           

          - Jon

          • 2. Re: How can I ignore multiple clicks while processing the first one?
            pramod.rao

            You can just disable the button after the first click and enable it back on after the process is complete.

             

            Hope this helps.

             

            -Pramod

            http://www.flexmycode.com

            • 3. Re: How can I ignore multiple clicks while processing the first one?
              JonInAms Level 1

              Thanks for the response Pramod. I tried to disable and enable the button but it doesn't seem to have any effect at all. If I click the button several times, this code still processs the clicks one at a time over the next few seconds. I am sure I am making a simple mistake somewhere here...

               

              private function clickHandler():void {

                 mybutton.enabled = false;

              ...slow stuff here

                 mybutton.enabled = true;

              }

               

              Full code...

              <?xml version="1.0"?>

              <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"

                    xmlns:s="library://ns.adobe.com/flex/spark"

                    xmlns:mx="library://ns.adobe.com/flex/mx">

                 <fx:Script>

                    <![CDATA[

               

              private var count:Number = 0;

               

              private function clickHandler():void {

               

                mybutton.enabled = false;

               

                 // Something which takes a while to complete...

                 var timer:Date = new Date();

                 while( (new Date()).valueOf() - timer.valueOf() <  2000 ) {

                    var nop:int = 0;

                 }

               

                  // Report result

                 count++;

                 output.text="Test Number "+count;  

               

                 mybutton.enabled=true;

              }

               

              ]]>

                 </fx:Script>

               

                  <s:Panel title="Example">

                    <s:VGroup left="10" right="10" top="10" bottom="10">

                       <s:Label id="output" text=""/>

                       <s:Button id="mybutton" label="Click Me" click="clickHandler();"/>

                    </s:VGroup>

                 </s:Panel>

               

              </s:Application>

                                       
              • 4. Re: How can I ignore multiple clicks while processing the first one?
                pramod.rao Level 1

                My Bad!! I didn't check what I was telling.. I don't know if it is possible though (unless there's an asynchronous call). However, you can also try adding an overlay on the button (or application) while you are running your jobs.

                 

                Do let us know, if you have figured this out already.

                Thanks,

                 

                -Pramod

                http://www.flexmycode.com