#[repr(C)]pub struct Timer {
pub data: RefAny,
pub node_id: OptionDomNodeId,
pub created: Instant,
pub last_run: OptionInstant,
pub run_count: usize,
pub delay: OptionDuration,
pub interval: OptionDuration,
pub timeout: OptionDuration,
pub callback: TimerCallback,
}Expand description
A Timer is a function that is run on every frame.
There are often a lot of visual Threads 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§
§data: RefAnyData that is internal to the timer
node_id: OptionDomNodeIdOptional node that the timer is attached to - timers attached to a DOM node will be automatically stopped when the UI is recreated.
created: InstantStores when the timer was created (usually acquired by Instant::now())
last_run: OptionInstantWhen the timer was last called (None only when the timer hasn’t been called yet).
run_count: usizeHow many times the callback was run
delay: OptionDurationIf the timer shouldn’t start instantly, but rather be delayed by a certain timeframe
interval: OptionDurationHow 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: OptionDurationWhen to stop the timer (for example, you can stop the
execution after 5s using Some(Duration::from_secs(5))).
callback: TimerCallbackCallback to be called for this timer
Implementations§
Source§impl Timer
impl Timer
Sourcepub fn new(
data: RefAny,
callback: TimerCallbackType,
get_system_time_fn: GetSystemTimeCallback,
) -> Self
pub fn new( data: RefAny, callback: TimerCallbackType, get_system_time_fn: GetSystemTimeCallback, ) -> Self
Create a new timer
pub fn tick_millis(&self) -> u64
Sourcepub fn is_about_to_finish(&self, instant_now: &Instant) -> bool
pub fn is_about_to_finish(&self, instant_now: &Instant) -> bool
Returns true ONCE on the LAST invocation of the timer This is useful if you want to run some animation and then when the timer finishes (i.e. all animations finish), rebuild the UI / DOM (so that the user does not notice any dropped frames).
Sourcepub fn instant_of_next_run(&self) -> Instant
pub fn instant_of_next_run(&self) -> Instant
Returns when the timer needs to run again
Sourcepub fn with_delay(self, delay: Duration) -> Self
pub fn with_delay(self, delay: Duration) -> Self
Delays the timer to not start immediately but rather start after a certain time frame has elapsed.
Sourcepub fn with_interval(self, interval: Duration) -> Self
pub fn with_interval(self, interval: Duration) -> Self
Converts the timer into a timer, running the function only
if the given Duration has elapsed since the last run
Sourcepub fn with_timeout(self, timeout: Duration) -> Self
pub fn with_timeout(self, timeout: Duration) -> Self
Converts the timer into a countdown, by giving it a maximum duration (counted from the creation of the Timer, not the first use).
Sourcepub fn invoke(
&mut self,
callback_info: CallbackInfo,
frame_start: Instant,
get_system_time_fn: GetSystemTimeCallback,
) -> TimerCallbackReturn
pub fn invoke( &mut self, callback_info: CallbackInfo, frame_start: Instant, get_system_time_fn: GetSystemTimeCallback, ) -> TimerCallbackReturn
Crate-internal: Invokes the timer if the timer should run. Otherwise returns
Update::DoNothing
Trait Implementations§
impl Eq for Timer
impl StructuralPartialEq for Timer
Auto Trait Implementations§
impl Freeze for Timer
impl RefUnwindSafe for Timer
impl Send for Timer
impl Sync for Timer
impl Unpin for Timer
impl UnwindSafe for Timer
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more