1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- package timer_test
- import (
- "fmt"
- "leafstalk/module/timer"
- "time"
- )
- func ExampleTimer() {
- d := timer.NewDispatcher(10)
- // timer 1
- d.AfterFunc(1, func() {
- fmt.Println("My name is Leaf")
- })
- // timer 2
- t := d.AfterFunc(1, func() {
- fmt.Println("will not print")
- })
- t.Stop()
- // dispatch
- (<-d.ChanTimer).Cb()
- // Output:
- // My name is Leaf
- }
- func ExampleCronExpr() {
- cronExpr, err := timer.NewCronExpr("0 * * * *")
- if err != nil {
- return
- }
- fmt.Println(cronExpr.Next(time.Date(
- 2000, 1, 1,
- 20, 10, 5,
- 0, time.UTC,
- )))
- // Output:
- // 2000-01-01 21:00:00 +0000 UTC
- }
- func ExampleCron() {
- d := timer.NewDispatcher(10)
- // cron expr
- cronExpr, err := timer.NewCronExpr("* * * * * *")
- if err != nil {
- return
- }
- // cron
- var c *timer.Cron
- c = d.CronFunc(cronExpr, func() {
- fmt.Println("My name is Leaf")
- c.Stop()
- })
- // d.CronFunc(cronExpr, func() {
- // fmt.Println("My name is Leaf")
- // })
- // dispatch
- // for {
- // (<-d.ChanTimer).Cb()
- // }
- (<-d.ChanTimer).Cb()
- // Output:
- // My name is Leaf
- }
|