Skip to content
This repository has been archived by the owner on Jun 9, 2023. It is now read-only.

Latest commit

 

History

History
91 lines (76 loc) · 6.25 KB

cp.deferred.md

File metadata and controls

91 lines (76 loc) · 6.25 KB

docs » cp.deferred


This extension makes it simple to defer multiple actions after a delay from the initial execution. Unlike hs.timer.delayed, the delay will not be extended with subsequent run() calls, but the delay will trigger again if run() is called again later.

For example:

local update = deferred.new(1) -- defer 1 second
:action(function() print("Updated!"") end)
-- do something
update()
-- do something else
update()
-- one second after the inital call to `update()`, one "Updated!" is printed.

API Overview

  • Constructors - API calls which return an object, typically one that offers API methods
  • new
  • Methods - API calls which can only be made on an object returned by a constructor
  • action
  • delay
  • run
  • secondsRemaining
  • stop
  • waiting

API Documentation

Constructors

Signature cp.deferred.new(delay) -> cp.deferred
Type Constructor
Description Creates a new defer instance, which will trigger any added actions by a set delay after
Parameters
  • delay - The number of seconds to delay when run() is initally called.
Returns
  • The new cp.deferred instance.

Methods

Signature cp.deferred:action(actionFn) -> self
Type Method
Description Adds the action the the list that will be called when the timer goes off.
Parameters
  • The callable action.

| Signature | cp.deferred:delay([value]) -> self | number | | -----------------------------------------------------|---------------------------------------------------------------------------------------------------------| | Type | Method | | Description | Sets/gets the delay period. If no value is provided, the current delay is returned. | | Parameters |

  • value - the new delay value.
| | Returns |
  • The cp.deferred instance if a new value is provided, or the current delay if not.
|

Signature cp.deferred:run() -> self
Type Method
Description Ensures that the actions will run after the delay.
Parameters
  • None
Returns
  • The cp.deferred instance.

| Signature | cp.deferred:secondsRemaining() -> number | nil | | -----------------------------------------------------|---------------------------------------------------------------------------------------------------------| | Type | Method | | Description | Returns the number of seconds until the next execution, or nil if it's not running. | | Parameters |

  • None
| | Returns |
  • The number of seconds until execution.
|

Signature cp.deferred:stop() -> self
Type Method
Description Stops any execution of any deferred actions, if it is currently running.
Parameters
  • None
Returns
  • The deferred timer.
Signature cp.deferred:waiting() -> boolean
Type Method
Description Checks if the defer is currently waiting to run.
Parameters
  • None
Returns
  • true if the deferred action is waiting to execute.