1

Say I have this code for a simple timer:

var a = document.getElementById("a")
var start = Date.now()
window.onload = function() {
  setInterval(()=>{
    a.innerHTML = ((Date.now() - start)/1000).toFixed(3)
  }, 1)
}
<p id="a">0.000</p>       
I've always wanted a timer that counts in multiples of 1/30, so therefore it would go .033, .067, .100, .167, etc. I cannot modify the setInterval just to do this because of how inaccurate it is, so how can I round what the current time is so that the decimal ends in a valid value?
I know this is terribly worded, I apologize
Komali
  • 196
  • 9
  • 2
    The wording is fine. This makes perfect sense. :-) Just multiply everything by 30, do integer math, and divide back out again for your displayed decimal value. – Brad Nov 24 '20 at 19:44
  • 1
    See also [How to create an accurate timer in javascript?](https://stackoverflow.com/q/29971898/1048572) – Bergi Nov 24 '20 at 20:12
  • 2
    Like @Brad said, `(Math.floor(diff * 30 / 1000) / 30).toFixed(3)` should do – Bergi Nov 24 '20 at 20:14
  • Perfect! This works great, thanks so much – Komali Nov 24 '20 at 20:55

0 Answers0