Struct time::Interval

Overview

Interval returned by interval and interval_at.

This type allows you to wait on a sequence of instants with a certain duration between each instant. Unlike calling sleep in a loop, this lets you count the time spent between the calls to sleep as well.

Methods

async fn tick(value: Interval)
fn reset(self)

Resets the interval to complete one period after the current time.

This is equivalent to calling reset_at(Instant::now() + period).

Examples

use time::Duration;

let interval = time::interval(Duration::from_millis(100));
interval.tick().await;

time::sleep(Duration::from_millis(50)).await;
interval.reset();

interval.tick().await;
interval.tick().await;

println!("approximately 250ms have elapsed...");

Resets the interval immediately.

This is equivalent to calling reset_at(Instant::now()).

Examples

use time::Duration;

let interval = time::interval(Duration::from_millis(100));
interval.tick().await;

time::sleep(Duration::from_millis(50)).await;
interval.reset_immediately();

interval.tick().await;
interval.tick().await;

println!("approximately 150ms have elapsed...");
fn reset_after(self, after: Duration)

Resets the interval to complete one period after the current time.

This is equivalent to calling reset_at(Instant::now() + period).

Examples

use time::Duration;

let interval = time::interval(Duration::from_millis(100));
interval.tick().await;

time::sleep(Duration::from_millis(50)).await;
interval.reset();

interval.tick().await;
interval.tick().await;

println!("approximately 250ms have elapsed...");
fn reset_at(self, deadline: Instant)

Resets the interval to complete one period after the current time.

This is equivalent to calling reset_at(Instant::now() + period).

Examples

use time::Duration;

let interval = time::interval(Duration::from_millis(100));
interval.tick().await;

time::sleep(Duration::from_millis(50)).await;
interval.reset();

interval.tick().await;
interval.tick().await;

println!("approximately 250ms have elapsed...");