I make a JS stopwatch that has format H:m:s:ms when it reached 60 min it always increase to 61,62 and so on
i am using setInterval here is my code:
n = 3600;
td = setInterval(function ()
{
n++;
let hours = parseInt(n / 3600);
var m = parseInt(n / 60)
var s = parseInt(n / 60 % 60);
var M = parseInt(n % 60);
// let hours = Math.floor(((n[ids] / 1000) / 60) / 60) % 24
// let m = Math.floor((n[ids] / 1000) / 60) % 60
// let s = Math.floor(n[ids] / 1000) % 60
// let M = Math.floor(n[ids] % 1000)
$("#display0").html(toDub(hours) + ":" + toDub(m) + ":" + toDub(s) + ":" + toDub(M))
}, 1000 / 60);
full source code: https://jsfiddle.net/greycat/danvL42s/2/
I am sure there is something wrong with my m variable.
Anyone can help me out?
3
Answers
The logic follows that a second is the rounded value for its fraction of 60 milliseconds, and a minute is the rounded value for its fraction of 60 seconds, so on and so forth.
You can refactor the time variables as below: