如何仅在循环中第一次调用函数

How to call function only for first time in loop

这里我有一组动画将在循环中继续,我试图仅在第一次迭代时调用该函数,当第一次迭代结束时,不应再调用该函数。我该怎么做?

这是我的代码

             var r5anim1 = function(){
                $(".r5").velocity({ 
                      opacity: 1,
                      scale: [1,0],
                    }, {
                    duration:300,
                    complete: function() {
                    r5anim2();
                    }
                });
             }  

            var r5anim2 = function(){
                $(".r5").velocity({ 
                  opacity: 0,
                },{
                    duration:200,
                    complete: function() {
                    r6anim1();
                    wishes() // here i have called the function which should execute only on first loop.
                }               
            });
            }

            var r6anim1 = function(){
                $(".r6").velocity({ 
                      opacity: 1,
                      scale: [1,0],
                    }, {
                    duration:300,
                    complete: function() {
                    r6anim2();
                    }
                });
             }  

            var r6anim2 = function(){
                $(".r6").velocity({ 
                  opacity: 0,
                },{
                    duration:200,
                complete: function() {                                         
                    r5anim1();       //on end this will start the loop again 
                }               
            });
            }




        /*****************this function should be called only for the first time******************/
        var wishes = function(){
            boolvalue = 0;
            $(".wishes").velocity({ 
                  opacity: 1,
                  scale: [1,0.4],
                }, {
                duration:600,
            });
         }  
        r5anim1();

创建全局变量

var isTriggered = false;

测试是否被触发,如果不是触发则调用函数

if(!isTriggered)
 {
wishes()
}

在 wishes() 函数中将变量更改为 true 以便循环停止

function wishes() {
isTriggered = false;
//other code
}

您必须创建一个全局可访问的变量并将其默认为 false

var isFunctionCalled = false;

仅当标志为 false 时才调用该函数,并在调用该函数时将标志更改为 true。

if(isFunctionCalled == false){
    wishes()
    isFunctionCalled = true;
}