Home > OS >  why doesn't setInterval get called until loop is over
why doesn't setInterval get called until loop is over

Time:07-25

In my game loop, I want to ask three questions. After that a setInterval should be called which works with the answers to those three questions. After this Interval is finished, I want to repeat this process 3 more times. However, when the loop starts, It goes through the questions 3 times and then does the setInterval part.

Edit: rewrote everything in English.

//require "prompt-sync"
const prompt = require("prompt-sync")({ sigint: true });

//Auto class
class Car{
    constructor(brand, speed, maxSpeed, currentSpeed){
        this.carBrand = brand;
        this.carSpeed = speed;
        this.carMaxSpeed = maxSpeed;
        this.carCurrentSpeed = currentSpeed;
    }

    drive(){
        if(this.carCurrentSpeed >= this.carMaxSpeed){
            clearInterval(inter);
            console.log("too fast");
        }else{
            this.carCurrentSpeed = this.carCurrentSpeed   this.carSpeed;
            console.log(this.carBrand   " has a speed of "   this.currentSpeed   " km/h");
        }
    }    
}


//gameloop
for(var i = 0; i < 3; i  ){
    //infos from usr
    let brand = prompt("what brand is your car? ");
    let speed = parseInt(prompt("How much speed does your car gain per second? "));
    let maxSpeed = parseInt(prompt("what is the maximum speed of your car? "));
    
    //create Object
    var usrAuto = new Car(brand, speed, maxSpeed, 0);

    //setInterval
    var inter = setInterval(usrAuto.plus.bind(usrAuto), 1000);

CodePudding user response:

New answer: You expect the code to run the interval and, once done, continue the code where it left. It doesn't work because setInterval doesn't stop the code, it makes an asynchronous task, that will run on each delay set.

What you can do is just a simple while loop and "sleep" for 1 second. As sleep doesn't exist on JavasScript, there are some ways to make a new one, here is one ofthe simplest ways.

 const prompt = require("prompt-sync")({ sigint: true });
function sleep(milliseconds) {
  const date = Date.now();
  let currentDate = null;
  do {
    currentDate = Date.now();
  } while (currentDate - date < milliseconds);
}

//Auto class
class Car{
    constructor(brand, speed, maxSpeed, currentSpeed){
        this.carBrand = brand;
        this.carSpeed = speed;
        this.carMaxSpeed = maxSpeed;
        this.carCurrentSpeed = currentSpeed;
        this.crashed = false;
    }
    drive(){
        if(this.carCurrentSpeed >= this.carMaxSpeed){
            console.log("too fast");
            this.crashed= true;
        }else{
            this.carCurrentSpeed = this.carCurrentSpeed   this.carSpeed;
            console.log(this.carBrand   " has a speed of "   this.carCurrentSpeed   " km/h");
        }
    }    
}

var cars = []
//gameloop
for(var i = 0; i < 3; i  ){
    //infos from usr
    let brand = prompt("what brand is your car? ");
    let speed = parseInt(prompt("How much speed does your car gain per second? "));
    let maxSpeed = parseInt(prompt("what is the maximum speed of your car? "));
    
    //create Object
    var usrAuto = new Car(brand, speed, maxSpeed, 0);
    while (usrAuto.crashed == false){
        usrAuto.drive()
        sleep(1000)
    }

}
  • Related