How to execute a function only if that function isn't already running?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • londres9b
    New Member
    • Apr 2010
    • 106

    How to execute a function only if that function isn't already running?

    I have a function that runs every time the user changes something. But sometimes the user will have to change multiple things and I don't want to call the function 2 or more times..
    How can I make it execute only if it isn't already running?
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    how did you define the onchange event?

    Comment

    • londres9b
      New Member
      • Apr 2010
      • 106

      #3
      It's a checkbox and whenever it's changed the function executes.
      Code:
      onchange="result()"

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        that actually fires? well, you can’t avoid that each checkbox fires when it is clicked. the only way out would be a final "calculate result now" button. (between (un)checking two checkboxes is too much time for the first event to know that there will be another event soon)

        you could try to define a blur event on the form, but that is not reliable, esp. if you don’t use a mouse (e.g. when tabbing through the options)

        Comment

        • londres9b
          New Member
          • Apr 2010
          • 106

          #5
          So there isn't any way to check if a function is running?

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            how long does your function run?

            regular functions run between 0 and 5 ms.

            Comment

            • londres9b
              New Member
              • Apr 2010
              • 106

              #7
              The function checks which checkboxes are checked and makes an ajax request to update a table.

              So you can see why I wanted to do this, I would wait for a second or two and then check which checkboxes are checked.

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                you can’t test whether a regular function currently runs (JavaScript would have to be multi-threaded to do that, which is just not the way it is programmed).

                in case of AJAX I see only the option to abort the first request, but unless you have a very slow internet connection or a massively complex server script behind, this will be finished before the second event occurs.

                maybe I could imagine some kind of request stacking with timeouts, though that sounds rather complicated.

                Comment

                • londres9b
                  New Member
                  • Apr 2010
                  • 106

                  #9
                  Maybe if I set the result() function to execute automatically every 3 seconds, instead of the onchange trigger..

                  Okey, thanks for your help.

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    but then you would make an AJAX request every 3 seconds (constantly adding up to your transfer volume and server load), no matter whether there was a checkbox changed or not.

                    Comment

                    • londres9b
                      New Member
                      • Apr 2010
                      • 106

                      #11
                      yes, I know, I'll leave at is was and include something to prevent the user from clicking the checkboxes while the function is running.

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        that would be mean towards the user. your function probably runs faster than the user clicks another option.

                        Comment

                        • londres9b
                          New Member
                          • Apr 2010
                          • 106

                          #13
                          Then I guess I'll go back to how it was

                          Comment

                          • rnd me
                            Recognized Expert Contributor
                            • Jun 2007
                            • 427

                            #14
                            Originally posted by londres9b
                            sometimes the user will have to change multiple things and I don't want to call the function 2 or more times..
                            How can I make it execute only if it isn't already running?

                            your function needs a few lines at the top and bottom:
                            Code:
                            function myChanger(){
                             if(arguments.callee.busy){return;}
                             arguments.callee.busy=true;
                             
                             //... now do the slow stuff like normal
                            
                             arguments.callee.busy = false;
                            }//end myChanger()
                            now, if it fires again while the middle is already running, it returns without proceeding further.

                            Comment

                            Working...