pub struct Timer(/* private fields */);
Implementations§
Source§impl Timer
impl Timer
Sourcepub fn stop(&self)
pub fn stop(&self)
Stop the timer (preventing execution of the callback in the future)
§Note
Another way to stop a timer is to drop every clone of the timer.
§Example
let timer = runner.timer(|| assert!(false));
timer.reset(Duration::from_millis(200));
timer.stop();
// callback is never executed
thread::sleep(Duration::from_millis(500));
Sourcepub fn reset(&self, duration: Duration)
pub fn reset(&self, duration: Duration)
Restart the timer, regardless of whether the timer is running or not. e.g. repeatably calling .reset(1 sec) will cause the timer to never fire.
§Arguments
duration
: duration until the callback should execute
§Example
let timer = runner.timer(|| assert!(false));
// the timer never fires
let dur = Duration::from_millis(200);
for _ in 0..5 {
timer.reset(dur);
thread::sleep(dur / 2);
}
// timer is dropped and cancelled
Sourcepub fn start(&self, duration: Duration) -> bool
pub fn start(&self, duration: Duration) -> bool
Start the timer, but only if the timer is not already pending e.g. if repeatably calling .start(1 sec), the timer will fire ~ once every second
§Arguments
duration
: duration until the callback should execute
§Returns
A bool indicating whether the timer was started (true) or already running (false).
§Example
let timer = runner.timer(|| println!("fired"));
// this timer will fire twice
let dur = Duration::from_millis(200);
for _ in 0..5 {
timer.start(dur);
thread::sleep(dur / 2);
}
// timer is dropped and cancelled
Sourcepub fn fire(&self)
pub fn fire(&self)
Manually cause the timer to fire immediately. This cancels any pending timeout (equivalent to calling .stop()) before executing the callback.
§Note
The callback is run in the calling thread
as oppose to being run by the thread in the associated Runner
instance.
§Example
let timer = runner.timer(|| println!("fired"));
timer.start(Duration::from_millis(200));
timer.fire();
// timer is fired immediately, not after 200ms.