pub struct TIMERS;Expand description
App timers, deadlines and timeouts.
You can use this service to create UI bound timers, these timers run using only the app loop and awake the app to notify updates.
Timer updates can be observed using variables that update when the timer elapses, or you can register
handlers to be called directly when the time elapses. Timers can be one-time, updating only once when
a deadline is reached; or they can update every time on a set interval.
Note that you can also use the task::deadline function to .await deadlines, in app
threads this function uses the TIMERS service too.
§Precision
Timers elapse at the specified time or a little later, depending on how busy the app main loop is. High frequency timers can also have an effective lower frequency of updates because timers only elapse once per frame cycle.
Implementations§
Source§impl TIMERS
impl TIMERS
Sourcepub fn deadline(&self, deadline: impl Into<Deadline>) -> DeadlineVar
pub fn deadline(&self, deadline: impl Into<Deadline>) -> DeadlineVar
Returns a DeadlineVar that will update once when the deadline is reached.
If the deadline is in the past the variable will still update once in the next app update.
Drop all clones of the variable to cancel the timer.
let deadline = TIMERS.deadline(20.secs());
text = deadline.map(|d| if d.has_elapsed() { "20 seconds have passed" } else { "..." });In the example above the deadline variable will update 20 seconds later when the deadline has_elapsed. The variable
is read-only and will only update once.
Sourcepub fn interval(&self, interval: Duration, paused: bool) -> TimerVar
pub fn interval(&self, interval: Duration, paused: bool) -> TimerVar
Returns a TimerVar that will update every time the interval elapses.
The timer can be controlled using methods in the variable value. The timer starts
running immediately if paused is false.
let timer = TIMERS.interval(1.secs(), false);
text = timer.map(|t| match t.count() {
0 => formatx!(""),
1 => formatx!("1 second elapsed"),
c => formatx!("{c} seconds elapsed"),
});In the example above the timer variable will update every second, the variable keeps a count
of times the time elapsed, that is incremented every update. The variable is read-only but the value can
be used to control the timer to some extent, see TimerVar for details.
Sourcepub fn on_deadline(
&self,
deadline: impl Into<Deadline>,
handler: Handler<DeadlineArgs>,
) -> DeadlineHandle
pub fn on_deadline( &self, deadline: impl Into<Deadline>, handler: Handler<DeadlineArgs>, ) -> DeadlineHandle
Register a handler that will be called once when the deadline is reached.
If the deadline is in the past the handler will be called in the next app update.
let handle = TIMERS.on_deadline(
20.secs(),
hn_once!(|_| {
println!("20 seconds have passed");
}),
);§Handler
The handler can be any of the once Handler<A> flavors. You can use the macros
hn_once! or async_hn_once!
to declare a handler closure.
Async handlers execute up to the first .await immediately when the deadline is reached, subsequent awakes
are scheduled like an async preview event handler.
§Handle
Returns a DeadlineHandle that can be used to cancel the timer, either by dropping the handle or by
calling cancel. You can also call perm
to drop the handle without cancelling.
Sourcepub fn on_interval(
&self,
interval: Duration,
paused: bool,
handler: Handler<TimerArgs>,
) -> TimerHandle
pub fn on_interval( &self, interval: Duration, paused: bool, handler: Handler<TimerArgs>, ) -> TimerHandle
Register a handler that will be called every time the interval elapses.
The timer starts running immediately if paused is false.
Auto Trait Implementations§
impl Freeze for TIMERS
impl RefUnwindSafe for TIMERS
impl Send for TIMERS
impl Sync for TIMERS
impl Unpin for TIMERS
impl UnwindSafe for TIMERS
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
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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