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

Create a new timer


Declaration

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

Parameters

  • 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

Return Value

A timer object. Call start() to begin the timer.

hs.timer.new

Create a new timer (alias for create())


Declaration

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

Parameters

  • 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

Return Value

A timer object. Call start() to begin the timer.

hs.timer.doAfter

Create and start a one-shot timer


Declaration

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

Parameters

  • seconds number

    Number of seconds to wait before firing

  • callback JSValue

    A JavaScript function to call when the timer fires

Return Value

A timer object (already started)

hs.timer.doEvery

Create and start a repeating timer


Declaration

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

Parameters

  • interval number

    The interval in seconds at which the timer should fire

  • callback JSValue

    A JavaScript function to call when the timer fires

Return Value

A timer object (already started)

hs.timer.doAt

Create and start a timer that fires at a specific time


Declaration

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

Parameters

  • 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

Return Value

A timer object (already started)

hs.timer.usleep

Block execution for a specified number of microseconds (strongly discouraged)


Declaration

hs.timer.usleep(microseconds) -> None

Parameters

  • microseconds number

    Number of microseconds to sleep

Return Value

None

hs.timer.secondsSinceEpoch

Get the current time as seconds since the UNIX epoch with sub-second precision


Declaration

hs.timer.secondsSinceEpoch() -> number

Parameters

None

Return Value

Fractional seconds since midnight, January 1, 1970 UTC

hs.timer.absoluteTime

Get the number of nanoseconds since the system was booted (excluding sleep time)


Declaration

hs.timer.absoluteTime() -> UInt64

Parameters

None

Return Value

Nanoseconds since boot

hs.timer.localTime

Get the number of seconds since local midnight


Declaration

hs.timer.localTime() -> number

Parameters

None

Return Value

Seconds since midnight in the local timezone

hs.timer.minutes

Converts minutes to seconds


Declaration

hs.timer.minutes(n) -> number

Parameters

  • n number

    A number of minutes

Return Value

The equivalent number of seconds

hs.timer.hours

Converts hours to seconds


Declaration

hs.timer.hours(n) -> number

Parameters

  • n number

    A number of hours

Return Value

The equivalent number of seconds

hs.timer.days

Converts days to seconds


Declaration

hs.timer.days(n) -> number

Parameters

  • n number

    A number of days

Return Value

The equivalent number of seconds

hs.timer.weeks

Converts weeks to seconds


Declaration

hs.timer.weeks(n) -> number

Parameters

  • n number

    A number of weeks

Return Value

The equivalent number of seconds

hs.timer.doUntil

Repeat a function/lambda until a given predicate function/lambda returns true


Declaration

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

Parameters

  • 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

Return Value

None

hs.timer.doWhile

Repeat a function/lambda while a given predicate function/lambda returns true


Declaration

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

Parameters

  • 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

Return Value

None

hs.timer.waitUntil

Wait to call a function/lambda until a given predicate function/lambda returns true


Declaration

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

Parameters

  • 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

Return Value

None

hs.timer.waitWhile

Wait to call a function/lambda until a given predicate function/lambda returns false


Declaration

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

Parameters

  • 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

Return Value

None