Skip to content
Advertisement

Does javascript switchs runs continuously [closed]

i was wondering if switches runs continuously or does not need of setIntervals to run (forever). i’m making a game where in a level value changes and the switch will change all entities position based on the level;

let LEVEL = 0;

switch (LEVEL) {
    case 1: 
    player.x = 64;
    player.y = 64;
    enemy.x = 32;
    enemy.y = 32;
    break;
    case 2: 
    player.x = 0;
    player.y = 64;
    enemy.x = 72;
    enemy.y = 56;
    break;
}

Advertisement

Answer

No. A switch is like every other statement in JavaScript: It only runs when it’s reached in the step-by-step execution of the code. Your example would run exactly once, for instance. To run more often would require that you put it in a loop (but then nothing else can run except that loop) or, as you said, call a function that contains the switch from a timer.

i’m making a game where in a level value changes and the switch will change all entities position based on the level

Put the switch in a function that you call when you change the level.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement