Global ObjectDate and TimeFactory Global

Stopwatch()

Measures elapsed time. Stopwatch() creates a stopwatch object; start(), elapsedMs() and lap() are called on that object.

Documented for app version 1.0.51Updated:

Detailed Explanation

This section explains when to use the API, how to call it, and which structures it works best with in production flow.

How To Call It

Stopwatch is a callable factory table: object methods are called as watch:<method>(...), and the table’s own functions as Stopwatch.<method>(...).

When To Use It

For time flow, combine DateTime, TimeSpan, and Stopwatch with logging, timeout control, and performance measurement.

Parameters and Return

Stopwatch offers 14 methods; each method page lists its parameters and return value.

Best Combined With

A strong combination is to measure with Stopwatch, log with DateTime, and compare durations with TimeSpan.

Example Usage

The snippet below is a starter pattern that can be applied directly in runtime flow.

local watch = Stopwatch()
watch:start()
wait(250)
print("Elapsed ms: " .. tostring(watch:elapsedMs()))

Copyable Progressive Examples

From foundation to combined usage, each level is provided as a separate code block so you can copy the level you need and adapt it directly.

Foundation

Shows the shortest direct way to call the API.

Foundation
local watch = Stopwatch()
watch:start()
wait(250)
print("Elapsed ms: " .. tostring(watch:elapsedMs()))

Simple

Wraps the base call with minimal flow control.

Simple
local watch = Stopwatch()
watch:start()
wait(250)
print("Elapsed ms: " .. tostring(watch:elapsedMs()))

-- Continue with one focused method on the object.

Practical Flow

A practical pattern for real macros with pcall, logging, and guards.

Practical Flow
local watch = Stopwatch()
watch:start()
wait(250)
print("Elapsed ms: " .. tostring(watch:elapsedMs()))
local note = "Stopwatch overview loaded"
print(note)

Detailed

This level packages the API into a reusable helper with error reporting.

Detailed
-- For time flow, combine DateTime, TimeSpan, and Stopwatch with logging, timeout control, and performance measurement.
local function run_stopwatch_step()
  local watch = Stopwatch()
  watch:start()
  wait(250)
  print("Elapsed ms: " .. tostring(watch:elapsedMs()))
end

local ok, err = pcall(run_stopwatch_step)
if not ok then
  toast("Step failed")
  print(err)
end

Combined

Combines the API with related structures to form a more realistic workflow.

Combined
local watch = Stopwatch()
watch:start()
wait(250)
print("Elapsed ms: " .. tostring(watch:elapsedMs()))
print("Elapsed ms: " .. tostring(watch:elapsedMs()))