API Docs

Module for creating and managing timers

Types

This module provides the following types:

Properties

hs.timer.doUntil

JSValue
Repeat a function until a predicate returns true. Swift-retained storage for the JS implementation.

hs.timer.doWhile

JSValue
Repeat a function while a predicate returns true. Swift-retained storage for the JS implementation.

hs.timer.waitUntil

JSValue
Wait to call a function until a predicate returns true. Swift-retained storage for the JS implementation.

hs.timer.waitWhile

JSValue
Wait to call a function until a predicate returns false. Swift-retained storage for the JS implementation.

Methods

hs.timer.create(interval, callback, continueOnError) -> HSTimer

Create a new timer
hs.timer.create(interval, callback, continueOnError) -> HSTimer
Name Type Description
interval number The interval in seconds at which the timer should fire
callback JSValue A JavaScript function to call when the timer fires
continueOnError boolean If true, the timer will continue running even if the callback throws an error
HSTimer
A timer object. Call start() to begin the timer.
const t = hs.timer.create(5, () => console.log("tick"), false)
t.start()

hs.timer.doAfter(seconds, callback) -> HSTimer

Create and start a one-shot timer
hs.timer.doAfter(seconds, callback) -> HSTimer
Name Type Description
seconds number Number of seconds to wait before firing
callback JSValue A JavaScript function to call when the timer fires
HSTimer
A timer object (already started)
hs.timer.doAfter(5, () => console.log("fired"))

hs.timer.doEvery(interval, callback) -> HSTimer

Create and start a repeating timer
hs.timer.doEvery(interval, callback) -> HSTimer
Name Type Description
interval number The interval in seconds at which the timer should fire
callback JSValue A JavaScript function to call when the timer fires
HSTimer
A timer object (already started)
hs.timer.doEvery(60, () => console.log("every minute"))

hs.timer.doAt(time, repeatInterval, callback, continueOnError) -> HSTimer

Create and start a timer that fires at a specific time
hs.timer.doAt(time, repeatInterval, callback, continueOnError) -> HSTimer
Name Type Description
time number Seconds since midnight (local time) when the timer should first fire
repeatInterval number If provided, the timer will repeat at this interval. Pass 0 for one-shot.
callback JSValue A JavaScript function to call when the timer fires
continueOnError boolean If true, the timer will continue running even if the callback throws an error
HSTimer
A timer object (already started)
// Fire at 9am every day
hs.timer.doAt(9 * 3600, 86400, () => console.log("morning"), false)

hs.timer.usleep(microseconds) -> None

Block execution for a specified number of microseconds (strongly discouraged)
hs.timer.usleep(microseconds) -> None
Name Type Description
microseconds number Number of microseconds to sleep
None
hs.timer.usleep(100000)  // 100ms

hs.timer.secondsSinceEpoch() -> number

Get the current time as seconds since the UNIX epoch with sub-second precision
hs.timer.secondsSinceEpoch() -> number
number
Fractional seconds since midnight, January 1, 1970 UTC
console.log(hs.timer.secondsSinceEpoch())

hs.timer.absoluteTime() -> UInt64

Get the number of nanoseconds since the system was booted (excluding sleep time)
hs.timer.absoluteTime() -> UInt64
UInt64
Nanoseconds since boot
console.log(hs.timer.absoluteTime())

hs.timer.localTime() -> number

Get the number of seconds since local midnight
hs.timer.localTime() -> number
number
Seconds since midnight in the local timezone
console.log(hs.timer.localTime())

hs.timer.minutes(n) -> number

Converts minutes to seconds
hs.timer.minutes(n) -> number
Name Type Description
n number A number of minutes
number
The equivalent number of seconds
console.log(hs.timer.minutes(5))  // 300

hs.timer.hours(n) -> number

Converts hours to seconds
hs.timer.hours(n) -> number
Name Type Description
n number A number of hours
number
The equivalent number of seconds
console.log(hs.timer.hours(2))  // 7200

hs.timer.days(n) -> number

Converts days to seconds
hs.timer.days(n) -> number
Name Type Description
n number A number of days
number
The equivalent number of seconds
console.log(hs.timer.days(1))  // 86400

hs.timer.weeks(n) -> number

Converts weeks to seconds
hs.timer.weeks(n) -> number
Name Type Description
n number A number of weeks
number
The equivalent number of seconds
console.log(hs.timer.weeks(1))  // 604800

hs.timer.doUntil(predicateFn, actionFn, checkInterval) -> None

Repeat a function/lambda until a given predicate function/lambda returns true
hs.timer.doUntil(predicateFn, actionFn, checkInterval) -> None
Name Type Description
predicateFn any A function/lambda to test if the timer should continue. Return True to end the timer, False to continue it
actionFn any A function/lambda to call until the predicateFn returns true
checkInterval any How often, in seconds, to call actionFn
None

hs.timer.doWhile(predicateFn, actionFn, checkInterval) -> None

Repeat a function/lambda while a given predicate function/lambda returns true
hs.timer.doWhile(predicateFn, actionFn, checkInterval) -> None
Name Type Description
predicateFn any A function/lambda to test if the timer should continue. Return True to continue the timer, False to end it
actionFn any A function/lambda to call while the predicateFn returns true
checkInterval any How often, in seconds, to call actionFn
None

hs.timer.waitUntil(predicateFn, actionFn, checkInterval) -> None

Wait to call a function/lambda until a given predicate function/lambda returns true
hs.timer.waitUntil(predicateFn, actionFn, checkInterval) -> None
Name Type Description
predicateFn any A function/lambda to test if the actionFn should be called. Return True to call the actionFn, False to continue waiting
actionFn any A function/lambda to call when the predicateFn returns true. This will only be called once and then the timer will stop.
checkInterval any How often, in seconds, to call predicateFn
None

hs.timer.waitWhile(predicateFn, actionFn, checkInterval) -> None

Wait to call a function/lambda until a given predicate function/lambda returns false
hs.timer.waitWhile(predicateFn, actionFn, checkInterval) -> None
Name Type Description
predicateFn any A function/lambda to test if the actionFn should be called. Return False to call the actionFn, True to continue waiting
actionFn any A function/lambda to call when the predicateFn returns False. This will only be called once and then the timer will stop.
checkInterval any How often, in seconds, to call predicateFn
None