pub fn cancel_timer(timer_id: u64) -> CmdExpand description
Creates a command that cancels a specific timer.
This command sends a CancelTimerMsg to the program, which will stop
the timer with the given ID. Use this with timer IDs returned by
every_with_id() to stop repeating timers.
§Arguments
timer_id- The ID of the timer to cancel
§Returns
A command that cancels the specified timer
§Examples
use bubbletea_rs::{command, Model, Msg, KeyMsg};
use crossterm::event::KeyCode;
use std::time::Duration;
struct MyModel {
timer_id: Option<u64>,
}
impl Model for MyModel {
fn init() -> (Self, Option<command::Cmd>) {
(Self { timer_id: Some(123) }, None)
}
fn update(&mut self, msg: Msg) -> Option<command::Cmd> {
// Cancel timer when user presses 's' for stop
if let Some(key_msg) = msg.downcast_ref::<KeyMsg>() {
if key_msg.key == KeyCode::Char('s') {
if let Some(id) = self.timer_id {
self.timer_id = None;
return Some(command::cancel_timer(id));
}
}
}
None
}
fn view(&self) -> String {
if self.timer_id.is_some() {
"Timer running. Press 's' to stop.".to_string()
} else {
"Timer stopped.".to_string()
}
}
}