timer
stableHigh-resolution stopwatch, countdown, and interval classes for timing operations, measuring elapsed time, and driving game loops or scheduled checks.
use plugin timer::{Timer.new, start, pause, …} Functions (19)
- Timer.new Create a new stopwatch timer
- start Start or resume timing
- pause Pause the timer, preserving elapsed time
- resume Resume a paused timer
- elapsed_ms Get elapsed time in milliseconds
- elapsed_secs Get elapsed time in seconds
- is_running Check if the timer is currently running
- reset Reset all state to zero
- lap Record a lap split and return its duration
- stop Stop and return total elapsed ms
- restart Reset and restart, returning previous elapsed ms
- Countdown.new Create a countdown from a duration in ms
- update Advance countdown state, returns true when finished
- remaining_ms Get remaining countdown time in ms
- is_finished Check if the countdown has completed
- progress Get countdown progress as a 0.0–1.0 fraction
- Interval.new Create a repeating interval ticker
- tick Check if the interval has fired since last call
- set_interval Change the interval duration while running
Create a new stopwatch timer
Creates a new high-resolution stopwatch. The timer starts in a paused state — call start() to begin measuring. Backed by std::time::Instant for sub-millisecond accuracy.
use plugin timer::{Timer}
let t = Timer.new()
t.start()
// ... do work ...
let ms = t.elapsed_ms()
print("Elapsed: {ms} ms")
Start or resume timing
Begins timing. If the timer is already running this is a no-op, so it is safe to call multiple times.
use plugin timer::{Timer}
let t = Timer.new()
t.start()
Pause the timer, preserving elapsed time
Pauses the timer, accumulating elapsed time so far. Resuming later picks up from where the timer left off.
use plugin timer::{Timer}
let t = Timer.new()
t.start()
t.pause()
let ms = t.elapsed_ms()
print("Paused at {ms} ms")
Resume a paused timer
Resumes a paused timer. If the timer is already running this is a no-op.
use plugin timer::{Timer}
let t = Timer.new()
t.start()
t.pause()
t.resume()
Get elapsed time in milliseconds
Returns the total accumulated time in milliseconds, including any time before the last pause.
use plugin timer::{Timer}
let t = Timer.new()
t.start()
print(t.elapsed_ms())
Get elapsed time in seconds
Returns the total accumulated time in seconds. Equivalent to elapsed_ms() / 1000.0.
use plugin timer::{Timer}
let t = Timer.new()
t.start()
print("{t.elapsed_secs()} seconds")
Check if the timer is currently running
Returns true if the timer is currently accumulating time.
use plugin timer::{Timer}
let t = Timer.new()
print(t.is_running())
t.start()
print(t.is_running())
Reset all state to zero
Resets the timer to zero and stops it. After reset, call start() to begin again.
use plugin timer::{Timer}
let t = Timer.new()
t.start()
t.reset()
print(t.elapsed_ms())
Record a lap split and return its duration
Records a lap split. Returns the time elapsed since the last lap (or since start if no previous lap). Updates the internal lap reference point.
use plugin timer::{Timer}
let t = Timer.new()
t.start()
// ... first segment ...
let lap1 = t.lap()
// ... second segment ...
let lap2 = t.lap()
print("Lap 1: {lap1} ms, Lap 2: {lap2} ms")
Stop and return total elapsed ms
Stops the timer and returns the total accumulated elapsed time in milliseconds.
use plugin timer::{Timer}
let t = Timer.new()
t.start()
let total = t.stop()
print("Total: {total} ms")
Reset and restart, returning previous elapsed ms
Resets and immediately restarts the timer. Returns the elapsed time from the previous run before the reset.
use plugin timer::{Timer}
let t = Timer.new()
t.start()
let previous = t.restart()
print("Previous run: {previous} ms")
Create a countdown from a duration in ms
Creates a countdown timer for the given duration in milliseconds. Call start() to begin and update() each frame to check for completion.
use plugin timer::{Countdown}
let cd = Countdown.new(5000.0)
cd.start()
Advance countdown state, returns true when finished
Checks whether the countdown has reached its duration. Returns true when the countdown finishes and transitions to the finished state. Call this once per frame in a game loop.
use plugin timer::{Countdown}
let cd = Countdown.new(3000.0)
cd.start()
// in game loop:
let done = cd.update()
if done {
print("Time is up!")
}
Get remaining countdown time in ms
Returns the number of milliseconds remaining before the countdown reaches zero.
use plugin timer::{Countdown}
let cd = Countdown.new(10000.0)
cd.start()
print("Remaining: {cd.remaining_ms()} ms")
Check if the countdown has completed
Returns true if the countdown has completed. Does not require update() to have been called.
use plugin timer::{Countdown}
let cd = Countdown.new(1000.0)
cd.start()
print(cd.is_finished())
Get countdown progress as a 0.0–1.0 fraction
Returns the fraction of the countdown that has elapsed, from 0.0 (just started) to 1.0 (finished). Useful for driving progress bars or animations.
use plugin timer::{Countdown}
let cd = Countdown.new(5000.0)
cd.start()
let pct = cd.progress() * 100.0
print("{pct}% complete")
Create a repeating interval ticker
Creates an interval that fires every interval_ms milliseconds. Call start() then poll tick() each frame to detect when the interval fires.
use plugin timer::{Interval}
let iv = Interval.new(500.0)
iv.start()
Check if the interval has fired since last call
Returns true once each time the interval period has elapsed. Call this every frame; it advances the internal reference point so the next call measures from the last fire time.
use plugin timer::{Interval}
let iv = Interval.new(1000.0)
iv.start()
// in game loop:
if iv.tick() {
print("One second passed")
}
Change the interval duration while running
Changes the interval duration while the interval may be running. The change takes effect on the next tick evaluation.
use plugin timer::{Interval}
let iv = Interval.new(1000.0)
iv.start()
iv.set_interval(250.0)