Whoa didn't know about deno repl. This is awesome. Happy 1.7 gs!
for (const [s, d] of (function*() { while (true) { const m=new Date().valueOf(), s=Math.ceil((m+10)/1000); yield [s, s*1000-m] } })()) { await new Promise(r => { setTimeout(()=>r(), d) }); console.log(s) }
setInterval(() => console.log(Math.ceil(Date.now()/1000)), 1000);0
This could be 0.999s late, but seems good enough for REPL usage
Suit yourself, though.
function t(){let m=Date.now(),s=Math.ceil((m+10)/1000);setTimeout(()=>{console.log(s);t()},s*1000-m)}t()
That maintains the accuracy, but frankly just a 1000ms interval will probably be good enough in most cases, though it’ll drift all over the second (e.g. it loses about a millisecond each tick on my machine under Node), and it’s certainly much simpler to reason about. async function* tickSeconds() {
while (true) {
const m = new Date().valueOf()
const s = Math.ceil((m + 10) / 1000)
await new Promise((resolve, _) => {
setTimeout(() => resolve(), s * 1000 - m)
})
yield s
}
}
for await (const s of tickSeconds()) {
console.log(s)
}
I prefer to inline some obvious stuff that is missing than do a paradigm shift to using callbacks or build up an ad hoc library of misc functions.