Mithun Raikar Mithun Raikar - 1 month ago 14
jQuery Question

How to call function only for first time in loop

Here i have set of animations that will continue in loop and i am trying to call the function only on the first iteration and when the first iteration ends,the function should not be called anymore. How can i do that?

Here is my code

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();

Answer

Create a global variable

var isTriggered = false;

test if isTriggered, if its not tigger call the function

if(!isTriggered)
 {
wishes()
}

change the variable to true in the wishes() function so the loops stops

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