Minutes, Seconds and Milliseconds with setInterval

0

Speak, I'm trying to make a simple stopwatch and I want to display Minutes, Seconds and Milliseconds, I can display minutes and seconds, but can anyone give me a light on milliseconds?

The method below runs every second, but if I put instead of 1000 put 1 the function will be executed every millisecond, how do I do the conversion calculation?

this.timer = setInterval(() => {
      this.minutos = Math.floor(++this.segundosTotais / 60 );
      this.segundos = this.segundosTotais - this.minutos * 60;
    }, 1000);
    
asked by anonymous 25.09.2018 / 00:25

1 answer

0
  

If I put instead of 1000 put 1 the function will be executed every millisecond

In theory yes, but in practice, no. The JavaScript engine does not handle executing 1000 times per minute functions passed to setInterval . At best, you'll get something like a run every 40ms. Additionally, setInterval will discard garbled executions that you can not handle. For more details on this, see Why do you say setTimeout recursion is better than setInterval? .

  

How do I do the conversion calculation?

You can not know with this code, because we do not know the type of your this object. If it were a date (for example, the return of new Date() ), it would be possible to call the getMilliseconds() ", which returns the milliseconds of the date value. If this object does not have the value with millisecond precision, just seconds, it will not be possible to display the milliseconds of the date.

    
25.09.2018 / 00:43