Basic Example

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gogf/gf/v2/os/gctx"
  6. "github.com/gogf/gf/v2/os/gtime"
  7. "github.com/gogf/gf/v2/os/gtimer"
  8. "time"
  9. )
  10. func main() {
  11. var (
  12. ctx = gctx.New()
  13. now = time.Now()
  14. )
  15. gtimer.AddTimes(ctx, time.Second, 10, func(ctx context.Context) {
  16. fmt.Println(gtime.Now(), time.Duration(time.Now().UnixNano()-now.UnixNano()))
  17. now = time.Now()
  18. })
  19. select {}
  20. }

After execution, the output is:

  1. 2021-05-27 13:28:19 1.004516s
  2. 2021-05-27 13:28:20 997.262ms
  3. 2021-05-27 13:28:21 999.972ms
  4. 2021-05-27 13:28:22 1.00112s
  5. 2021-05-27 13:28:23 998.773ms
  6. 2021-05-27 13:28:24 999.957ms
  7. 2021-05-27 13:28:25 1.002468s
  8. 2021-05-27 13:28:26 997.468ms
  9. 2021-05-27 13:28:27 999.981ms
  10. 2021-05-27 13:28:28 1.002504s

Singleton Task

  1. package main
  2. import (
  3. "context"
  4. "github.com/gogf/gf/v2/os/gctx"
  5. "github.com/gogf/gf/v2/os/glog"
  6. "github.com/gogf/gf/v2/os/gtimer"
  7. "time"
  8. )
  9. func main() {
  10. var (
  11. ctx = gctx.New()
  12. interval = time.Second
  13. )
  14. gtimer.AddSingleton(ctx, interval, func(ctx context.Context) {
  15. glog.Print(ctx, "doing")
  16. time.Sleep(5 * time.Second)
  17. })
  18. select {}
  19. }

After execution, the output is:

  1. 2021-11-14 11:50:42.192 {189cwi9mo40cfp73guzhugo100tnuedg} doing
  2. 2021-11-14 11:50:48.190 {189cwi9mo40cfp73guzhugo100tnuedg} doing
  3. 2021-11-14 11:50:54.192 {189cwi9mo40cfp73guzhugo100tnuedg} doing
  4. 2021-11-14 11:51:00.189 {189cwi9mo40cfp73guzhugo100tnuedg} doing
  5. ...

Delayed Task

Delayed tasks refer to scheduled tasks that take effect after a specified time. We can create delayed tasks using DelayAdd* related methods.

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gogf/gf/v2/os/gctx"
  6. "github.com/gogf/gf/v2/os/gtime"
  7. "github.com/gogf/gf/v2/os/gtimer"
  8. "time"
  9. )
  10. func main() {
  11. var (
  12. ctx = gctx.New()
  13. delay = time.Second
  14. interval = time.Second
  15. )
  16. fmt.Println("Start:", gtime.Now())
  17. gtimer.DelayAdd(
  18. ctx,
  19. delay,
  20. interval,
  21. func(ctx context.Context) {
  22. fmt.Println("Running:", gtime.Now())
  23. },
  24. )
  25. select {}
  26. }

After execution, the terminal output is:

  1. Start: 2021-05-27 13:26:02
  2. Running: 2021-05-27 13:26:04
  3. Running: 2021-05-27 13:26:05
  4. Running: 2021-05-27 13:26:06
  5. Running: 2021-05-27 13:26:07
  6. Running: 2021-05-27 13:26:08
  7. Running: 2021-05-27 13:26:09
  8. Running: 2021-05-27 13:26:10
  9. Running: 2021-05-27 13:26:11
  10. ...

SetTimeout and SetInterval

These two methods are common scheduling methods from JavaScript. SetTimeout is used to create a scheduled task that executes only once. However, you can achieve infinite interval execution through recursive calls to SetTimeout. SetInterval is used to create scheduled tasks that execute at intervals without exiting.

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gogf/gf/v2/os/gctx"
  6. "github.com/gogf/gf/v2/os/gtime"
  7. "github.com/gogf/gf/v2/os/gtimer"
  8. "time"
  9. )
  10. func main() {
  11. var (
  12. ctx = gctx.New()
  13. timeout = time.Second
  14. interval = time.Second
  15. )
  16. gtimer.SetTimeout(ctx, timeout, func(ctx context.Context) {
  17. fmt.Println("SetTimeout:", gtime.Now())
  18. })
  19. gtimer.SetInterval(ctx, interval, func(ctx context.Context) {
  20. fmt.Println("SetInterval:", gtime.Now())
  21. })
  22. select {}
  23. }

After execution, the terminal output is:

  1. SetInterval: 2021-05-27 13:20:50
  2. SetTimeout: 2021-05-27 13:20:50
  3. SetInterval: 2021-05-27 13:20:51
  4. SetInterval: 2021-05-27 13:20:52
  5. SetInterval: 2021-05-27 13:20:53
  6. SetInterval: 2021-05-27 13:20:54
  7. SetInterval: 2021-05-27 13:20:55
  8. SetInterval: 2021-05-27 13:20:56
  9. SetInterval: 2021-05-27 13:20:57
  10. SetInterval: 2021-05-27 13:20:58
  11. ...

Exit Method to Exit

We can use the Exit method in scheduled tasks to forcefully exit the continuation of the task, which will then be removed from the scheduler.

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/gogf/gf/v2/os/gctx"
  6. "github.com/gogf/gf/v2/os/gtime"
  7. "github.com/gogf/gf/v2/os/gtimer"
  8. "time"
  9. )
  10. func main() {
  11. var (
  12. ctx = gctx.New()
  13. )
  14. gtimer.SetInterval(ctx, time.Second, func(ctx context.Context) {
  15. fmt.Println("exit:", gtime.Now())
  16. gtimer.Exit()
  17. })
  18. select {}
  19. }

After execution, the terminal output is:

  1. exit: 2021-05-27 13:31:24