chrisguttandin/worker-timers

What's the accuracy?

Specy opened this issue · 3 comments

Specy commented

I need a pretty precise timeout that i use as a delay function inside my music composer app, with standard setTimeout i get +-10ms of error, with timeouts being of around 50/200 ms each, would using this increase the accuracy?

Hi @Specy, I recently made a test and the accuracy was indeed a bit higher. However I would recommend testing it yourself directly in your app.

If possible I would also recommend not using setTimeout at all if it really needs to be precise and use the scheduling capabilities of the Web Audio API instead.

Specy commented

In the end i used this method which gives me a pretty accurate +-1ms error which is totally acceptable, i need to do some UI things (i'm using react) so the webaudio api wouldn't be of much help on that.

            let pastError = 0
            let previousTime = new Date().getTime()
            while (this.state.isPlaying) {
                const { song, settings } = this.state
                let msPerBPM = Math.floor(60000 / settings.bpm.value) + pastError
                previousTime = new Date().getTime()
                await delayMs(msPerBPM)   
                this.handleTick()
                pastError = previousTime + msPerBPM - new Date().getTime()
            }

i'm still going to use your module just to have that little better performance, thanks for making it and for helping out!

Thanks for reporting back. I'm happy that it works for you.