8000 Callback skipped · Issue #41 · mrchimp/tock · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content
Callback skipped #41
Open
Open
@jneuendorf

Description

@jneuendorf

Hey,

I've got the problem that sometimes a callback is skipped.

I created a minimal example where I get the current tick with Math.floor(tock.lap() / tock.interval)*.
I'd expect the list of ticks to be a list natural numbers.
But for some reason a tick is skipped at some point. Surprisingly, exactly until then the inaccuracy grows (see the console output).

Here's the example code:

import Tock from 'tocktimer'

const interval = 50
let lastTick = -1
let numberSkipped = 0

const clock = new Tock({
    interval,
    callback: () => {
        const elapsed = clock.lap()
        const tick = Math.floor(elapsed / interval)
        console.log('current tick:', tick, elapsed / interval, elapsed)
        if (tick !== lastTick + 1) {
            console.error(`skipped a callback. current tick=${tick}`)
            numberSkipped++
        }
        console.log('off by', elapsed - tick*interval, 'number of skipped ticks', numberSkipped)
        console.log('====================')
        lastTick = tick
    }
})

clock.start()

Is my assumption simply wrong or do you agree that this should be an invariant?

currentTick === lastTick + 1

Ok. I looked into it a bit more and the problem is the initial inaccuracy (until the 1st skipped tick).
Because of that the tick value without rounding (per callback) is growing from the correct value to value + 1. Here an example:

[
// (actualTick, tickValue, roundedTickValue, flooredTickValue)
(0, 0.00, 0, 0),
(1, 1.15, 1, 1), // always +1.15 => inaccuracy is 0.15 per callback
(2, 2.30, 2, 2),
(3, 3.45, 3, 3),
(4, 4.60, 5, 4), // this is where rounding 'breaks' (3 -> 5)
(5, 5.75, 6, 5),
(6, 6.90, 7, 6),
(7, 8.05, 8, 8), // this is where flooring 'breaks' (6 -> 8)
// after this the value is correct enough so that no gaps happen...
]

An obvious workaround would be to have a closured tick variable that just gets incremented but the fact that all ticks before the 1st 'error' occurs are relatively inaccurate and all following ticks are good (as I would expect from the library) is weird...

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0