Skip to content

Latest commit

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date

Repository files navigation

Advanced Timer

Advanced Timer is a Go package that provides a multi-thread safe wrapper around the time.Timer struct with a few extra features. It allows you to start, pause, resume, and stop a timer, as well as get the remaining time, maximum duration, and start time.

Features

  • Start() starts the timer.
  • Pause() pauses the timer.
  • Resume() resumes the timer.
  • Stop() stops the timer.
  • Finished channel is closed when the timer is stopped.
  • Remaining is the remaining time on the timer.
  • MaxDuration is the maximum duration of the timer.
  • StartedAt is the time the timer was started.
  • Paused is a boolean indicating whether the timer is paused or not.

Limitations

  • The remaining time has a margin of error of around 30 milliseconds.
  • The finished event is only guaranteed to be fired within 100 microseconds of the timer being stopped.

Usage

Create a new timer

maxDuration:=10*time.Secondtimer:=NewAdvancedTimer(maxDuration)

Start the timer

timer.Start()

Pause the timer

timer.Pause()

Resume the timer

imer.Resume()

Stop the timer

timer.Stop()

Get the remaining time

remaining:=timer.Remaining

Get the maximum duration

axDuration:=timer.MaxDuration

Check if the timer is paused

paused:=timer.Paused

Wait for the timer to finish

<-timer.IsFinished()

Get the time the timer was started

startedAt:=timer.StartedAt

Get a string representation of the timer

str:=timer.Stringfy()

Example

package main
import (
"fmt""time""github.com/user/advanced_timer"
)
funcmain() {
maxDuration:=5*time.Secondtimer:=advanced_timer.NewAdvancedTimer(maxDuration)
timer.Start()
fmt.Println(timer.Stringfy())
time.Sleep(2*time.Second)
timer.Pause()
fmt.Println(timer.Stringfy())
time.Sleep(2*time.Second)
timer.Resume()
fmt.Println(timer.Stringfy())
<-timer.IsFinished()
fmt.Println(timer.Stringfy())
}

buy me a coffee

Thank you for using the Advanced Timer Go package! If you found it useful and would like to support further development, you can buy me a coffee using the link below. Your support will help keep this package up-to-date and ensure that it continues to be a reliable solution for your Go timer needs. Thank you for your support!

Buy me a coffee

My GitHub

About

Advanced Timer is a Go package that provides a multi-thread safe wrapper around the time.Timer struct with a few extra features. It allows you to start, pause, resume, and stop a timer, as well as get the remaining time, maximum duration, and start time.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages