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

A scheduled timeout.

See Timeout::new for scheduling new timeouts.

Once scheduled, you can drop the Timeout 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 Timeout

source

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

Schedule a timeout to invoke callback in millis milliseconds from now.

Example
use gloo_timers::callback::Timeout;

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

pub fn forget(self) -> JsValue

Forgets this resource without clearing the timeout.

Returns the identifier returned by the original setTimeout call, and therefore you can still cancel the timeout by calling clearTimeout directly (perhaps via web_sys::clear_timeout_with_handle).

Example
use gloo_timers::callback::Timeout;

// We definitely want to do stuff, and aren't going to ever cancel this
// timeout.
Timeout::new(1_000, || {
    // Do stuff...
}).forget();
source

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

Cancel this timeout so that the callback is not invoked after the time is up.

The scheduled callback is returned.

Example
use gloo_timers::callback::Timeout;

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

// If actually we didn't want to set a timer, then cancel it.
if nevermind() {
    timeout.cancel();
}

Trait Implementations§

source§

impl Debug for Timeout

source§

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

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

impl Drop for Timeout

source§

fn drop(&mut self)

Disposes of the timeout, dually cancelling this timeout by calling clearTimeout directly.

Auto Trait Implementations§

§

impl !RefUnwindSafe for Timeout

§

impl !Send for Timeout

§

impl !Sync for Timeout

§

impl Unpin for Timeout

§

impl !UnwindSafe for Timeout

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.