pub struct Interval { /* private fields */ }
Expand description

A scheduled interval.

See Interval::new for scheduling new intervals.

Once scheduled, you can drop the Interval to clear it or forget to leak it. Once forgotten, the interval will keep running forever. This pattern is known as Resource Acquisition Is Initialization (RAII).

Implementations§

source§

impl Interval

source

pub fn new<F>(millis: u32, callback: F) -> Intervalwhere F: 'static + FnMut(),

Schedule an interval to invoke callback every millis milliseconds.

Example
use gloo_timers::callback::Interval;

let interval = Interval::new(1_000, move || {
    // Do something...
});
source

pub fn forget(self) -> JsValue

Forget this resource without clearing the interval.

Returns the identifier returned by the original setInterval call, and therefore you can still cancel the interval by calling clearInterval directly (perhaps via web_sys::clear_interval_with_handle).

Example
use gloo_timers::callback::Interval;

// We want to do stuff every second, indefinitely.
Interval::new(1_000, || {
    // Do stuff...
}).forget();
source

pub fn cancel(self) -> Closure<dyn FnMut()>

Cancel this interval so that the callback is no longer periodically invoked.

The scheduled callback is returned.

Example
use gloo_timers::callback::Interval;

let interval = Interval::new(1_000, || {
    // Do stuff...
});

// If we don't want this interval to run anymore, then cancel it.
if nevermind() {
    interval.cancel();
}

Trait Implementations§

source§

impl Debug for Interval

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Drop for Interval

source§

fn drop(&mut self)

Disposes of the interval, dually cancelling this interval by calling clearInterval directly.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.