timer 1:
package main
import (
"time"
"log"
)
func main() {
ticker := time.NewTicker(1* time.Second)
defer func() {
ticker.Stop()
log.Println("ticker Stop!")
}()
count := 0
for {
select {
case <-ticker.C:
count++
log.Println("wake up ... ", count)
if count >= 10 {
return
}
}
}
return
}
timer 2:
package main
import (
"log"
"time"
)
func main() {
timerWait := time.Second * 2
t := time.NewTimer(timerWait)
defer t.Stop()
for {
select {
case <-t.C:
// 需要重置Reset 使 t 重新开始计时
t.Reset(timerWait)
log.Println("wake up ...")
}
}
}