[][src]Struct azul_core::task::Timer

pub struct Timer {
    pub created: Instant,
    pub last_run: Option<Instant>,
    pub delay: Option<Duration>,
    pub interval: Option<Duration>,
    pub timeout: Option<Duration>,
    pub callback: TimerCallback,
}

A Timer is a function that is run on every frame.

There are often a lot of visual tasks such as animations or fetching the next frame for a GIF or video, etc. - that need to run every frame or every X milliseconds, but they aren't heavy enough to warrant creating a thread - otherwise the framework would create too many threads, which leads to a lot of context switching and bad performance.

The callback of a Timer should be fast enough to run under 16ms, otherwise running timers will block the main UI thread.

Fields

created: Instant

Stores when the timer was created (usually acquired by Instant::now())

last_run: Option<Instant>

When the timer was last called (None only when the timer hasn't been called yet).

delay: Option<Duration>

If the timer shouldn't start instantly, but rather be delayed by a certain timeframe

interval: Option<Duration>

How frequently the timer should run, i.e. set this to Some(Duration::from_millis(16)) to run the timer every 16ms. If this value is set to None, (the default), the timer will execute the timer as-fast-as-possible (i.e. at a faster framerate than the framework itself) - which might be performance intensive.

timeout: Option<Duration>

When to stop the timer (for example, you can stop the execution after 5s using Some(Duration::from_secs(5))).

callback: TimerCallback

Callback to be called for this timer

Implementations

impl Timer[src]

pub fn new(callback: TimerCallbackType) -> Self[src]

Create a new timer

pub fn with_delay(self, delay: Duration) -> Self[src]

Delays the timer to not start immediately but rather start after a certain time frame has elapsed.

pub fn with_interval(self, interval: Duration) -> Self[src]

Converts the timer into a timer, running the function only if the given Duration has elapsed since the last run

pub fn with_timeout(self, timeout: Duration) -> Self[src]

Converts the timer into a countdown, by giving it a maximum duration (counted from the creation of the Timer, not the first use).

pub fn invoke<'a>(&mut self, info: TimerCallbackInfo<'a>) -> TimerCallbackReturn[src]

Crate-internal: Invokes the timer if the timer and the self.timeout allow it to

Trait Implementations

impl Clone for Timer[src]

impl Copy for Timer[src]

impl Debug for Timer[src]

impl Eq for Timer[src]

impl Hash for Timer[src]

impl PartialEq<Timer> for Timer[src]

impl StructuralEq for Timer[src]

impl StructuralPartialEq for Timer[src]

Auto Trait Implementations

impl RefUnwindSafe for Timer

impl Send for Timer

impl Sync for Timer

impl Unpin for Timer

impl UnwindSafe for Timer

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.