How JavaScript code execution order works?

78 views Asked by At

When i used setInterval i encountered a problem, order of executing code suprised me:

var attack1 = setInterval(function(){
        weapon1.style.visibility = "visable";
        weapon1.animate(attack1Animation, attackTiming);
        weapon1.style.visibility = "hidden";
    }, 10000/unit1.attack_speed);

weapon1.style.visibility = "hidden"; is executing faster than weapon1.animate(attack1Animation, attackTiming);

I know we have LIFO and FIFO in JS. Asynchronus JS and functions (i am not sure if all kind of functions) execute after other code. So i tried something like this:

function notVisable() {
        weapon1.style.visibility = "hidden";
    }

    var attack1 = setInterval(function(){
        weapon1.style.visibility = "visable";
        weapon1.animate(attack1Animation, attackTiming);
        notVisable();
    }, 10000/unit1.attack_speed);

without those 2 lines everything is still working:

  • weapon1.style.visibility = "visable";
  • weapon1.style.height = "200px";

Animation works, so it's not fault of the animation.

Probably i would somehow manage to fix it, but i want to unterstand how it works :D

0

There are 0 answers