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
  • Definition
  • Description
  • Example

Was this helpful?

  1. Advanced

Callback

Function called every interval

PreviousDefinitionNextInterval

Last updated 1 year ago

Was this helpful?

useInterval(callback [, interval] [, options]): controls

Definition

Javascript
(ticks) => void
TypeScript
(ticks?: number) => void

Description

ticks is provided as first arugment to callback if is turned on, otherwise it is undefined

ticks is the number of that elapsed since last callback. For most cases it will be equal to 1 but if there is some bigger drift between calls (e.g. user swittched tab for a longer period of time and then came back) it gives you the opportunity to properly react to amount of intervals passed since the last call.

Example

Let's say you're using this hook for timer that updates every second, normally it would change every second from 00:00 to 00:01 then 00:02 etc. but if user leaves your tab inactive when timer shows 00:02 and then come back 30 seconds later, you will want to update your timer to 00:32 not 00:03 .

That's where ticks come into play because next callback will be called with ticks equal to 30, since last callback was 30 intervals (each 1s) ago, so you can properly increment your timer by 30s.

ticks are relative to so if in the example above your interval would be 500ms instead of 1000ms (1s) then ticks would be 60 instead of 30

interval
self correction
intervals