Skip to main content

use_interval

Function use_interval 

Source
pub fn use_interval<F>(millis: i32, callback: F) -> IntervalHandle
where F: FnMut() + 'static,
Expand description

Creates a recurring interval that invokes the given closure at the specified period, returning an IntervalHandle that is automatically cleared when the hook context is cleared (i.e., when the component unmounts or a match arm switches).

Unlike calling set_interval_with_callback_and_timeout_and_arguments_0

  • Closure::forget() manually, this hook ensures the interval is properly cleaned up, preventing memory leaks and stale callbacks.

The interval is only created once on the first render. On subsequent re-renders at the same hook index, the existing handle is returned unchanged.

§Arguments

  • i32 - The interval period in milliseconds.
  • FnMut() + 'static - The closure to invoke on each interval tick.

§Returns

  • IntervalHandle - A handle that can be used to cancel the interval early.

§Panics

Panics if window() is unavailable on the current platform.