Counter

Here’s a way to put a counter that updates once a second in a page. It’s based on the time.

This isn’t some earth-shattering discovery, it’s trival. But somebody asked me.

<script>
window.setInterval ( function tick () {
  const counter = document.getElementById('counter')
  if (counter) {
    const count = Math.trunc(0.001 * (Date.now() - Date.parse('2023-05-30T00:00:00')))
    counter.innerText = count
    console.log(count)
  }
}, 1000)
</script>
<p><span id="counter">123</span> is the count now!</p>

123 is the count now!

Leave a Comment