Minutes, Seconds and Milliseconds with setInterval

Asked

Viewed 1,056 times

0

Talk guys, I’m trying to make a simple chronometer and I want to display Minutes, seconds and milliseconds, I can display minutes and seconds, but someone can give me a light on the milliseconds?

the method below runs every second, but if I put instead of 1000 put 1 the function will run every millisecond, as I do the conversion calculation?

this.timer = setInterval(() => {
      this.minutos = Math.floor(++this.segundosTotais / 60 );
      this.segundos = this.segundosTotais - this.minutos * 60;
    }, 1000);
  • Convert what?

  • It would be more interesting if you gave more details than you would like to do. I think you’re a little confused.

  • I want to make a stopwatch that displays minutes, seconds and milliseconds, well, similar to the native Android stopwatch where the user enters with the minutes you want from now on I will do some color manipulations, record data etc...

1 answer

1

if I put instead of 1000 put 1 the function will run every millisecond

In theory yes, but in practice no. The Javascript engine does not account for running 1000 times per second the functions passed to setInterval. At best, you will achieve something like an execution every 40ms. Also, the setInterval will rule out blocked executions that can not meet. For more details about this, see Why do they say that recursive setTimeout is better than setInterval?.

how I do the conversion calculation

You can’t tell with this code, because we don’t know the type of your object this. If it were a date (for example, the return of new Date()), it would be possible to call the method getMilliseconds(), that returns the milliseconds of the date value. If this object does not have the value with milliseconds accuracy, only seconds, it will not be possible to display milliseconds of the date.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.