Skip to main content

Stopwatch Example

Lua Automation IDE

This Lua code demonstrates the usage of a stopwatch. The code begins by creating an instance of the stopwatch class using the factory method Stopwatch() and assigns it to the variable sw.

-- An example of how to use a the Stopwatch.
local sw = factory.Stopwatch()

ui.Log("Starting the stop watch now...")

-- Start the Stopwatch
sw.Start()

-- Pause for 1.5 seconds (it might differ by a few millseconds which would be the
-- small overhead from start, stop and pause)
ui.Pause(1500)

-- Stop the stopwatch
sw.Stop()

ui.Log("Completed in " .. sw.ElapsedMilliseconds .. "ms (" .. sw.ElapsedMilliseconds / 1000 .. "s)")

-- Start but don't reset
sw.Start()

-- Pause a half second
ui.Pause(500)

ui.Log("Completed in " .. sw.ElapsedMilliseconds .. "ms (" .. sw.ElapsedMilliseconds / 1000 .. "s)")

-- Reset the value
sw.Reset()

-- We'll see it's now at zero.
ui.Log("Stopwatch has been reset, current values are: " .. sw.ElapsedMilliseconds .. "ms (" .. sw.ElapsedMilliseconds / 1000 .. "s)")
Last Modified: 06/29/2023