-1

I want to have a set amount of time between my explosion function and the reset function, if I don't have a set amount of time, the code quickly executes the reset function after the execution function. This makes the explosion basically invisible. I tried setting a timeOut on the reset function, but that didn't change anything.

Here is the code:

  function loop(){
    //game code up here
    if(out){
      //run explode for certain amount of time then call reset function
      explode();
      //setTimeout(reset, time)
      reset();
    }
  }
  setInterval(loop, 10)

Crupeng
  • 317
  • 2
  • 14

2 Answers2

0

In JavaScript 1.7, using yield with async.js, you can do the following:

    function loop(){
    //game code up here
    if(out){
      //run explode for certain amount of time then call reset function
      explode();
      //setTimeout(reset, time)
      yield to.sleep(.500)
      reset();
    }
  }

If you want to use it only with plain javascript, you can try following,

function loop() {
    //game code up here
    if (out) {
        explode();
    }
}

function explode() {
    //explode code goes here
    setTimeout(reset, 3000);
}
loop();
Channa
  • 3,267
  • 7
  • 41
  • 67
0

As @Channa mentioned in above answer setTimeout will execute your reset function, once after 1000 milliseconds. Better if you wrap it like this, otherwise your reset function will run straight away.

setTimeout(function() { reset(); }, 1000);

In addition, if you want to call your function repeatedly, you can use this.

setInterval(reset,1000);

setInterval will call your reset function after every 1000 ms.

Thilina Koggalage
  • 1,044
  • 8
  • 16