Hello guys, I am trying to make stopwatch.
As per my understanding 1s = 1000ms so when i run this below code, it should increase second++ after 999ms but it increase after 10s
var s =0;
var ms =0;
function startWatch(){
ms++;
if(ms>999){
s++;
ms=0;
}
document.getElementById("demo").innerHTML = s+":"+ms;
setInterval(startWatch,1);
I tried the code above and was getting it to increase many times per second. I’m guessing that is not your entire code (the code provided was missing a } to end the function). So it may be some other issue with your complete code.
Keep in mind that there may be delays when using intervals and using intervals smaller than 4ms isn’t really going to work. See the doc linked here: