Skip to content

Timers #3

@henry-brisson

Description

@henry-brisson

Decouple timer interval from tick duration. Have timers have their own interval setting (default 1000ms), settable via constructor or setter.

Relevant README:


Meter Update

When a meter is instantiated, it does nothing (sits idly) until values are requested:

var meter = new Meter({current: 50.0, maximum: 100.0, regeneration: 1.0})
// wait 10 seconds
meter.amount // => 50.0 -- no regeneration
meter.current() // => 60.0 -- regenerates first, then returns `meter.amount`

The main difference being:

  • Calling most methods will cause the meter to regenerate.
  • Calling properties will return the stored values without changes.

If you'd rather not call current() to cause meter.amount to change, you can start a timer on the meter:

meter.startTimer()

This will create an internal timer that will constantly update meter.amount. The default interval is 1000 ms. If you would like to use a different interval, pass it as a parameter:

meter.startTimer({interval: 1500})

If you would like the timer to synchronize with the tick duration (so "whenever it ticks, it regenerates"), you can do so by:

meter.startTimer({synchronize: true})

Please note that by synchronizing the timer, the interval will be the same as the tick duration. If the tick duration is 1ms (the default), it may cause performance issues. A tick duration above 100ms is recommended.

To stop the timer:

meter.stopTimer()

Timers are great in combination with reactive libraries (such as React, Vue, etc) referencing meter.amount directly (as opposed to meter.current()) while the internal timer updates its value over time. You might also want to call meter.stopTimer() when the component is destroyed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions