Skip to content

timer.Start calls timer.tick() too frequently #867

@minkezhang

Description

@minkezhang

Describe the bug
The timer package's documentation does not accurately describe the actual behavior of the timer.

Setup
N/A

To Reproduce

t := timer.New(5 * time.Second)

t.Start()
t.Start() // According to documentation, this second start should have no effect

time.Sleep(time.Second + delta) // Just for demo purposes

fmt.Println(t.Timeout) // 3 * time.Second

Source Code
timer.startStop fires off a StartStopMsg message which in turn causes timer.Update to enqueue timer.tick. timer.tick in turn enqueues TickMsg. timer.Update assumes each tick is fired at exact intervals; therefore, calling timer.tick multiple times artificially speeds up the timer.

CS

Expected behavior
See documentation, which states calling timer.Start multiple times will have no effect. This is the expected behavior for a normal developer, but is not what actually happens.

A suggested fix will be to record the time of the last time a TickMsg message has been processed in timer.Update and subtract the duration (recommended in any case), or do not issue a StartStopMsg when timer.running matches the startStop input.

Screenshots
N/A

Additional context
The example suggested reset functionality as described in the example CS is also inaccurate. This assumes the timer is already in the run state and does nothing if the timer has already stopped.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions