hs.timer
ModuleModule 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
Declaration
hs.timer.create(interval, callback, continueOnError) -> HSTimer
Parameters
| 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 |
Returns
HSTimer
A timer object. Call start() to begin the timer.
Example
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
Declaration
hs.timer.doAfter(seconds, callback) -> HSTimer
Parameters
| Name | Type | Description |
|---|---|---|
| seconds | number | Number of seconds to wait before firing |
| callback | JSValue | A JavaScript function to call when the timer fires |
Returns
HSTimer
A timer object (already started)
Example
hs.timer.doAfter(5, () => console.log("fired"))
hs.timer.doEvery(interval, callback) -> HSTimer
Create and start a repeating timer
Declaration
hs.timer.doEvery(interval, callback) -> HSTimer
Parameters
| 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 |
Returns
HSTimer
A timer object (already started)
Example
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
Declaration
hs.timer.doAt(time, repeatInterval, callback, continueOnError) -> HSTimer
Parameters
| 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 |
Returns
HSTimer
A timer object (already started)
Example
// 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)
Declaration
hs.timer.usleep(microseconds) -> None
Parameters
| Name | Type | Description |
|---|---|---|
| microseconds | number | Number of microseconds to sleep |
Returns
None
Example
hs.timer.usleep(100000) // 100ms
hs.timer.secondsSinceEpoch() -> number
Get the current time as seconds since the UNIX epoch with sub-second precision
Declaration
hs.timer.secondsSinceEpoch() -> number
Returns
number
Fractional seconds since midnight, January 1, 1970 UTC
Example
console.log(hs.timer.secondsSinceEpoch())
hs.timer.absoluteTime() -> UInt64
Get the number of nanoseconds since the system was booted (excluding sleep time)
Declaration
hs.timer.absoluteTime() -> UInt64
Returns
UInt64
Nanoseconds since boot
Example
console.log(hs.timer.absoluteTime())
hs.timer.localTime() -> number
Get the number of seconds since local midnight
Declaration
hs.timer.localTime() -> number
Returns
number
Seconds since midnight in the local timezone
Example
console.log(hs.timer.localTime())
hs.timer.minutes(n) -> number
Converts minutes to seconds
Declaration
hs.timer.minutes(n) -> number
Parameters
| Name | Type | Description |
|---|---|---|
| n | number | A number of minutes |
Returns
number
The equivalent number of seconds
Example
console.log(hs.timer.minutes(5)) // 300
hs.timer.hours(n) -> number
Converts hours to seconds
Declaration
hs.timer.hours(n) -> number
Parameters
| Name | Type | Description |
|---|---|---|
| n | number | A number of hours |
Returns
number
The equivalent number of seconds
Example
console.log(hs.timer.hours(2)) // 7200
hs.timer.days(n) -> number
Converts days to seconds
Declaration
hs.timer.days(n) -> number
Parameters
| Name | Type | Description |
|---|---|---|
| n | number | A number of days |
Returns
number
The equivalent number of seconds
Example
console.log(hs.timer.days(1)) // 86400
hs.timer.weeks(n) -> number
Converts weeks to seconds
Declaration
hs.timer.weeks(n) -> number
Parameters
| Name | Type | Description |
|---|---|---|
| n | number | A number of weeks |
Returns
number
The equivalent number of seconds
Example
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
Declaration
hs.timer.doUntil(predicateFn, actionFn, checkInterval) -> None
Parameters
| 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 |
Returns
None
hs.timer.doWhile(predicateFn, actionFn, checkInterval) -> None
Repeat a function/lambda while a given predicate function/lambda returns true
Declaration
hs.timer.doWhile(predicateFn, actionFn, checkInterval) -> None
Parameters
| 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 |
Returns
None
hs.timer.waitUntil(predicateFn, actionFn, checkInterval) -> None
Wait to call a function/lambda until a given predicate function/lambda returns true
Declaration
hs.timer.waitUntil(predicateFn, actionFn, checkInterval) -> None
Parameters
| 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 |
Returns
None
hs.timer.waitWhile(predicateFn, actionFn, checkInterval) -> None
Wait to call a function/lambda until a given predicate function/lambda returns false
Declaration
hs.timer.waitWhile(predicateFn, actionFn, checkInterval) -> None
Parameters
| 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 |
Returns
None