timers

timer

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
package main

import (
	"fmt"
	"time"
)

func main() {
	timer1 := time.NewTimer(2 * time.Second)
	fmt.Println("Waiting...")
	<-timer1.C
	fmt.Println("timer1 expired")

	_ = time.AfterFunc(2*time.Second, func() {
		fmt.Println("timer2 expired")
	})

	fmt.Println("Waiting...")
	time.Sleep(2010 * time.Millisecond) // 2.01s > 前面的 2s

	fmt.Println("End")
}
最后修改 October 10, 2024: 更新 (a4b8f85)