Crate gloo_timers

source ·
Expand description

Working with timers on the Web: setTimeout and setInterval.

These APIs come in two flavors:

  1. a callback style (that more directly mimics the JavaScript APIs), and
  2. a Futures and Streams API.


Timeouts fire once after a period of time (measured in milliseconds).

Timeouts with a Callback Function

use gloo_timers::callback::Timeout;

let timeout = Timeout::new(1_000, move || {
    // Do something after the one second timeout is up!

// Since we don't plan on cancelling the timeout, call `forget`.

Timeouts as Futures

With the futures feature enabled, a future module containing futures-based timers is exposed.

use gloo_timers::future::TimeoutFuture;
use wasm_bindgen_futures::spawn_local;

// Spawn the `timeout` future on the local thread. If we just dropped it, then
// the timeout would be cancelled with `clearTimeout`.
spawn_local(async {
    // Do something here after the one second timeout is up!


Intervals fire repeatedly every n milliseconds.

Intervals with a Callback Function


Intervals as Streams



Callback-style timer APIs.
Future- and Stream-backed timers APIs.