Jump to content

Timer at beginning of state


Daria K
 Share

Recommended Posts

Hello everyone,

I am trying to create a timer that will count down three minutes after the state was started. I am not sure how to make a text that will visualize the timer.(00:00) Can you guys help out? 

Link to comment
Share on other sites

Hello,

I'm not a pro, but I would initialise some timer in init function to 0

this.timer = 0;

Then, in update function I will increment it using

    if (this.timer >= 1000) {
        this.secondsCounter++;
        this.timer-=1000;
        if(this.secondsCounter == 60) {
            this.secondsCounter-=60;
            this.minutesCounter++;
        }
    }

 

Link to comment
Share on other sites

Thank you for your answer, I got your idea, but what Im actually confused about is how to visualize it as a text showing 03:00 at the beginning and getting down like 02:59, 02:58.. and so on.. Any idea?

 

Link to comment
Share on other sites

If you want to go overkill you could use moment.js to help out, this is an over-the-top example to create a countdown

var moment = require( 'moment' )

var time = moment( 1000 * 60 * 3 )
 
function countdown( time ) {
  console.log( time.format( 'mm:ss' ) )

  if ( time <= 0 ) {
    return
  }

  setTimeout( () => countdown( time.subtract( 1000 ) ), 1000 )
}

countdown( time )

It's over the top for what you need though and isn't particularly accurate either (JS timeouts aren't very precise).

 

Link to comment
Share on other sites

8 minutes ago, Daria K said:

Thank you for your answer, I got your idea, but what Im actually confused about is how to visualize it as a text showing 03:00 at the beginning and getting down like 02:59, 02:58.. and so on.. Any idea?

 

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...