Callback

Function called every interval

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

Definition

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

Description

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

ticks is the number of intervals 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.

Last updated

Was this helpful?