2

I am working on a decimal clock (100min₁₀ / hr & 100s₁₀ / min₁₀). The code is skipping decimal seconds (in Firefox and Chrome in Ubuntu 14 & Android). The delay in setTimeout is 1.

function updateTime() {
    var now = new Date()
    var h = now.getHours();
    var m = now.getMinutes();
    var s = now.getSeconds();
    document.getElementById('babylonian').innerHTML = h+":"+padDigit(m)+":"+padDigit(s)
    document.getElementById('decimal').innerHTML = h + "h" + padDigit( Math.round( ( 100 * m ) / 60) ) + "." + padDigit( Math.round( ( 100 * s ) / 60 ) )

    setTimeout(updateTime, 1);
}

function padDigit(i) {
    return i<10 ? '0' + i : i
}

updateTime()

The clocks seem to be ticking in sync. I don't understand why.

Teemu
  • 22,918
  • 7
  • 53
  • 106
dysbulic
  • 3,005
  • 2
  • 28
  • 48

1 Answers1

4

This is because there are 100 "decimal seconds" per minute, but only 60 seconds. So for each second there's 100/60 "decimal seconds".

When you base the computation on now.getSeconds() you'll get leaps in the "decimal seconds".

Work with something finer grained as well such as now.getMilliseconds():

var s = now.getSeconds() + now.getMilliseconds()/1000;
...
...  ...  ... Math.floor(  s * 100  / 60 )  ...

(And do the same for minutes to avoid skipping decimal minutes.)

aioobe
  • 413,195
  • 112
  • 811
  • 826