Timer goes from 0:59 to 2:60

var GameBarScript = pc.createScript('gameBarScript');

GameBarScript.attributes.add('timerTxt', {type: 'entity'});

var minutes=0;

var sec=0;

var counter=0;

GameBarScript.prototype.initialize = function() {

this.GameBarScript = 0;

minutes = 0;

sec = 0;

time = setInterval(this.timeIt.bind(this), 1000);

this.timerTxt.element.text = minutes + ":" + sec;

};

GameBarScript.prototype.timeIt = function() {

sec = this.GameBarScript++;

if(sec>59){

    minutes++;

    this.GameBarScript = 0;

}

    this.timerTxt.element.text = minutes + ":" + sec;

};

This is the code I used for my timer. It only shows 0:0 for timerTxt instead of 00:00. And when it counts, it goes from 0:59 to 2:60 instead of 1:00. How do I fix this? I’m only a beginner and I’ve checked all forums so linking me to another forum won’t be of any help.

Hey, you are not updating the sec variable when sec > 59 and that’s why it says 60 and not 00

2 Likes

How do I do that?

If I’m correct, you have to add sec = 0; on line 30 of the script above.

1 Like