Prevent Javascript from being executed twice

前端 未结 5 1081
清歌不尽
清歌不尽 2020-12-19 13:04

I have a script that I am developing that creates a sliding button type effect. Five div\'s are situated next to eachother each with a link. When one of those DIVS are click

相关标签:
5条回答
  • 2020-12-19 13:35
    (function() {
    var mutex = false;
    
        function mnuClick(x){
            if (!mutex) {
               mutex = !mutex;
    
               /* all code here ...   */
    
               mutex = !mutex; /* this statement should go into animation callback */
            }
    
        }
    
    })();
    

    mantain a state through a variable so you cannot click more than once until code has fully executed

    0 讨论(0)
  • 2020-12-19 13:37

    you can create an invisible maskin and maskout ( like the background in lightbox etc ) or disable clicks until the animation finishes.

    0 讨论(0)
  • 2020-12-19 13:48

    you can unplug the onClick event handler (mnuClick) when the event starts, to effectively disable invoking the mnuClick twice, but be sure to restore it when the event ends.

    0 讨论(0)
  • 2020-12-19 13:54

    Quick answer: use .addClass and .removeClass and test for the existence of the class at execution time. Test if it's set and return, or add it, execute the code, then remove it.

    0 讨论(0)
  • 2020-12-19 14:00

    You need a "isRunning" flag. Check for it before you start. Set it when you start the sequence, clear it when it ends.

    0 讨论(0)
提交回复
热议问题