Is there a sleep/wait?

279 views Asked by At

My objective is to test a timer to check and see if the idea will work before I implement it into my RPG Maker project. Originally, there's a building block method in the software to make the timer/clock, but I'm trying to move on from that and create a JavaScript code to do just that. Thing is, I need it to pause for a brief moment, so the timer doesn't break. It'll be a day/night cycle in the game.

The test timer will be used to check and see if the idea works, and if not to debug. So far it works, but I am unable to pause it.

var min = 0;
var hour = 0;
var timer = true;

alert("Testing Timer");

    do{
        sec += 10; //Needs to pause for 5 secs after this line
        console.log(sec);
            if(sec >= 60){
                alert("It's " + hour + " : " + min + " : " + sec);
                min += 5;
                sec = 0;
                if(min >= 60)
                {
                    hour += 1;
                    min = 0;
                }
            }
    }while(timer === true);```
2

There are 2 answers

0
iams0nus On

you can use the setTimeout function to add a delay of 5 secs(5000 millisecs)

setTimeout(()=> {your code here}, 5000);
0
Muhammad Iqbal On
function wait(milliscond) {
  return new Promise(resolve => setTimeout(resolve, milliscond));
}

var min = 0;
var hour = 0;
var timer = true;

alert("Testing Timer");

    do{
        sec += 10; //Needs to pause for 5 secs after this line
        await wait(5000);
        console.log(sec);
            if(sec >= 60){
                alert("It's " + hour + " : " + min + " : " + sec);
                min += 5;
                sec = 0;
                if(min >= 60)
                {
                    hour += 1;
                    min = 0;
                }
            }
    }while(timer === true);