Skip to content
ludlovianPublic

About

SImple timer handling

Resources

Stars

0 stars

Watchers

1 watching

Forks

Latest commit

 

History

45 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

timer

Simple timer handling.

Does little more than wrap the standard API but keeps hold of the timeout entity.

Timer

new Timer(config)

Creates and configures a new timer.

config should be an object with the following:

  • ms: the delay after which the function should be called
  • fn: the function to call
  • repeat: if truthy, the timer should repeat every ms
  • inactive: if truthy, it only configures without starting

It can also accept the convenience properties:

  • after: the ms value and sets repeat to false
  • every: the ms value and sets repeat to true
  • at: sets the ms to fire at the given time, with repeat set to false

If no configuration is given, an inactive default is created which can be re-configured later.

Note: The underlying timer cannot accept a timeout of more than 2^31-1 ms. Or about 24 days. This can, and will wake every ~24 days until it is due. As a result it can accept Infinity as a ms setting.

.set(config) => this

Reconfigures the timer with new settings in the same way as new Timer

Any active timer is cancelled.

.ms => Number

Enumerable & read only. The current millisecond setting

.fn => Function

Enumerable & read only. The current function to call

.repeat

Enumerable & read only. The current repeat setting

.cancel() => this

Cancels the timer. Returns itself for chaining. Idempotent.

.refresh() => this

Restarts the timer. Returns itself

.active => Boolean

Is the timer currently active.

.due => Date | undefined

When is the timer next due. If ms was set to Infinity, or the timer is not active, returns undefined

.started => Date | undefined

When was the timer last started, or undefined if not active

.when => Promise

Returns a promise which will be resolved on the next timer callback.

If the timer is cancelled, then the promise will be rejected.

If the timer is inactive, then you'll simply get a resolved one.

.unref() => this

If in node and active, unref's the timer. Benign in the browser. Returns itself for chaining. This setting is maintained through repeats and manual refreshes

.ref() => this

The opposite of .unref. Safe in the browser.

.fire() => this

Manually fires the timer immediately, if active.

About

SImple timer handling

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages