2

I am trying to setState on a given time interval (100ms). I have discovered both in practice and through research that setInterval is inaccurate, and will float over time. I need it to be accurate because I'm aiming to have data (from an array) in my app synced up to a video.

Here is what I currently have:

data = this.processCSV(text);
    let idx = 0
    this.intOne = setInterval(() => {
      this.setState(previousState => (
        {
          data1: parseFloat(data[idx][2]),
          data2: parseFloat(data[idx][1]),
          data3: parseFloat(data[idx][3])
        }
      ));
      idx = idx + 1;
      }, 100);

In my research, I've found a few solutions that use Date to accurately keep track of time and account for drifting.

Expected Results: setState is called 10 times per second for an extended amount of time

Current Results: setState is called ~9 times per second and continues to float away from expected time

redsoxfan
  • 49
  • 3
  • 1
    No web api provides you such a guarantees. Even worse - given your application runs in a non rtos, the guarantees simply cannot be given. – zerkms Apr 08 '19 at 01:50
  • Is there anything that can get me close? I guess a perfect guarantee is unrealistic, but having something better than setInterval would be nice. – redsoxfan Apr 08 '19 at 01:52
  • what if you rethink the whole problem: instead of relying on number of ticks, you rely on the current video timeline and infer your state from it. – zerkms Apr 08 '19 at 01:54
  • I like the idea, not sure how I would achieve this though... – redsoxfan Apr 08 '19 at 02:13
  • Possible duplicate of [How to create an accurate timer in javascript?](https://stackoverflow.com/questions/29971898/how-to-create-an-accurate-timer-in-javascript) – skyboyer Apr 08 '19 at 02:23
  • This library seems to be keeping an accurate count and limits drifting: https://www.npmjs.com/package/accurate-interval – redsoxfan Apr 08 '19 at 02:42
  • @redsoxfan, what exact issue do you have trying to implement self-correcting timer? – skyboyer Apr 08 '19 at 04:49
  • I was having trouble because I kept getting an error that setState wasn’t a function. And I was having trouble figuring out where to put “.bind(this)” but figured it out – redsoxfan Apr 09 '19 at 02:34

0 Answers0