Breaking a loop, Javascript?

Hello,
Im wanting to call a particle animation only once, it is in the function update. Would I do something like this:

function Update () {
     if (gear = 1) {
          SomeParticleAnimation.Play();
          Break;
     }
     if (gear = 2) {
          SomeParticleAnimation.Play();
          Break;
     }
     //ect...
}

I want to play an particle animation every time the gear changes.

Thanks
Daniel

I take the time to accept and rate good answers!

Update is a function that is called within the loop and is not a loop itself.

You can exit the function by calling

return;

But this seems like a bad practice and would probably produce error prone code. Just use switch on your

gear

variable and do what is apropriate. This was your code is encapsulated and you can do other things that you may need in that function.

Switch is essentially same as if statements but is easier to read than a huge blob of ifs.

switch(gear) {
  case 1:
    //Do your stuff
    break;
  case 2:
    //Do your stuff
    break
}

Don’t use Update for this; it’s for things that happen every single frame. Instead just run code when you actually need it. It depends on where you’re actually changing the “gear” variable, but wherever that is, that’s where you put the code.

function SomeFunctionInWhichYouChangeGear () {
    if (gear == 1) {
        SomeParticleAnimation.Play();
    }
    ...
}

As @Lovrenc said… Update() will keep on calling…
Better to have a seperate function that controls the anims and which is called from update and which you can control how it is executed

You can do something like this:

var lastGear:int;// stores the value of last gear
var gear:int; // current gear
function Update()
{
  GearAnimation();
}

function GearAnimation()
{
   if(lastGear == gear) // did the gear change? no then return to exit
      return;

   lastGear = gear;

   // then use switch or if statments to check the value (switch will suit this more)
   switch(lastGear)
   {
     case 1:
       SomeParticleAnimation.Play();
       break;
     case 2:
     // etc....
   }

}

==EDIT==

What @Eric5h5 is saying is that instead of checking the gear value every frame (i.e. using the Update()) to play an animation once you should play the animation when the gear changes which what you will need to do eventually anyway.

As an example:

// You will call this function only when the gear value need to change
function ChangeGear(gearValue: int)
{
   gear = gearValue;

   // play the corresponding animation for the current value ( using switch/ if statements)
   switch(gear)
   {
     case 1:
       SomeParticleAnimation.Play();
       break;
     case 2:
     // etc....
   }

   // do other things if any...etc

}