React interval hook
  • Introduction
  • Installation
  • Basics
  • Advanced
    • Definition
    • Callback
    • Interval
    • Options
      • Configuration
        • Auto start
        • Immediate callback call
        • Self correction
      • Callbacks
        • On finish
    • Hook result / Controls
      • Start
      • Stop
      • Is active
  • Examples
    • Advanced usage
    • Live demo
      • Version 1
  • Migration
    • Change log
Powered by GitBook
On this page
  1. Advanced
  2. Options
  3. Configuration

Self correction

PreviousImmediate callback callNextCallbacks

Last updated 1 year ago

Was this helpful?

CtrlK

Was this helpful?

Name

selfCorrecting

Type

boolean

Default value

true

Description

Self correct time intervals between subsequent callback invocations to reflect actual time elapsed.

setTimeout and setInterval are not precise (explanation) which make them unusable in their basic form for functionalities that strictly depend on precise timing (like clocks, timers or time based events).

This option helps eliminating that problem by depending on absolute timestamps and auto adjusting time between each interval.

If you disable that option you will get same behaviour as using barebone setInterval but still using the advantage of control methods.

Example / Explanation

Let's say you set interval to 1000 ms. Internally it will set setTimeout(..., 1000).

But then it turns out that setTimeout actually took 1005 ms to call callback instead of 1000 ms. Now the self correction kicks in and the next setTimeout will be automatically set to 995 ms to compensate for a 5 ms drift.

This process will be repeated every interval.

But what if setTimeout take 5000 ms instead of 1005 ms? Not to worry, this case can be handled in callback by using ticks to indicate how many intervals has passed since last callback.