Accuracy of setTimeout in React Native

后端 未结 2 421
一个人的身影
一个人的身影 2021-01-19 02:05

I\'m building a metronome in React Native. After playing a click, I set a setTimeout for the next click. The timing however, is awful.

I did the followi

相关标签:
2条回答
  • 2021-01-19 02:49

    I've investigated almost any React Native solution for playing audio and various approaches of using javascript setTimeout()/setInterval(), but none of them were satisfactory in terms of time stability and accuracy.

    Probably the only way to go at the moment is to glue some native module like this: https://developer.apple.com/library/content/samplecode/HelloMetronome/Introduction/Intro.html to js side, as described in RN Docs: https://facebook.github.io/react-native/docs/native-modules-ios.html, which gives a pretty decent result, but unfortunately, it's iOS only, of course.

    0 讨论(0)
  • 2021-01-19 02:59

    The delay that you configure for setTimeout and setInterval should never be thought of as an exact delay time. They represent the "minimum" amount of time that you'll wait before the function is executed. This is because the user-agent places the callback into the event queue when the time has elapsed, but if the user-agent is still working on some other task, the callback will sit there.

    You should also know that there is a built-in absolute minimum time that you are never going to be able to go under of roughly 9-14ms. This is due to the internals of the user-agent.

    0 讨论(0)
提交回复
热议问题