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.
- 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.
- 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.
maxDuration:=10*time.Secondtimer:=NewAdvancedTimer(maxDuration)timer.Start()timer.Pause()imer.Resume()timer.Stop()remaining:=timer.RemainingaxDuration:=timer.MaxDurationpaused:=timer.Paused<-timer.IsFinished()startedAt:=timer.StartedAtstr:=timer.Stringfy()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())
}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!
