Message from JavaScript discussions

April 2019

— Is there any way to wait for a for loop to finish without callback?

— 

let time = null;

class Game {
constructor(pace, money){
this.startTime = 464;
this.pace = pace;
this.money = money;
this.interval = null;
}
hour(a){
a = Math.floor(this.startTime/60)
return (a > 23) ?a = 0 :a
};
minute(a){
a = this.startTime%60
return (a < 10) ?'0'+a :a }
displayTime(){
return this.hour() +':'+this.minute()
}
tick(){
return this.startTime++
}
play(...func){

const queue = (...func)=>{
for(let f of func){
f()
}
}

this.interval = setInterval(()=>{
console.log(this.displayTime());
queue(...func)
this.tick()
console.log(time);
time++;}, 1000)
}
pause(){
return clearInterval(this.interval)
}
resume(){
this.pause()
this.play()
}
manage(actions, duration) {
this.pause();
this.play(
...actions.length === 0
? [
() =>
console.error('no function was called')
]
: actions.slice(0, 3));
setTimeout(
() => this.resume(),
duration);
}
}
const newGame = new Game(1000, 10000);
time = newGame.startTime

const test =()=>newGame.pause()

setTimeout(test, 10000)

const test2 = () => console.log('walking')
const test3 = () => console.log('talking')
const test5 = () => console.log('shopping')

newGame.play()

const testing = () => newGame.manage([test2, test3], 2000)
setTimeout(testing, 3000)


When you have a moment TRGWII I would be very thankful if you could tell me if this code looks clean enough 🙏

Thanks in advance

— I got it now, many thanks

— Wat

— Fix the time global!

— Should be this.time

— Time is later on a reference for all other classes

— If you look in my code you'll find

time = newGame.startTime

— Yeah, it's a global

— Not good

— I'm not sure how to resolve this tbh

— Make it per-class