github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/testdata/timers.go (about) 1 package main 2 3 import "time" 4 5 var timer = time.NewTimer(time.Millisecond) 6 7 func main() { 8 // Test ticker. 9 ticker := time.NewTicker(time.Millisecond * 500) 10 println("waiting on ticker") 11 go func() { 12 time.Sleep(time.Millisecond * 150) 13 println(" - after 150ms") 14 time.Sleep(time.Millisecond * 200) 15 println(" - after 200ms") 16 time.Sleep(time.Millisecond * 300) 17 println(" - after 300ms") 18 }() 19 <-ticker.C 20 println("waited on ticker at 500ms") 21 <-ticker.C 22 println("waited on ticker at 1000ms") 23 ticker.Stop() 24 time.Sleep(time.Millisecond * 750) 25 select { 26 case <-ticker.C: 27 println("fail: ticker should have stopped!") 28 default: 29 println("ticker was stopped (didn't send anything after 750ms)") 30 } 31 32 timer := time.NewTimer(time.Millisecond * 750) 33 println("waiting on timer") 34 go func() { 35 time.Sleep(time.Millisecond * 200) 36 println(" - after 200ms") 37 time.Sleep(time.Millisecond * 400) 38 println(" - after 400ms") 39 }() 40 <-timer.C 41 println("waited on timer at 750ms") 42 time.Sleep(time.Millisecond * 500) 43 44 reset := timer.Reset(time.Millisecond * 750) 45 println("timer reset:", reset) 46 println("waiting on timer") 47 go func() { 48 time.Sleep(time.Millisecond * 200) 49 println(" - after 200ms") 50 time.Sleep(time.Millisecond * 400) 51 println(" - after 400ms") 52 }() 53 <-timer.C 54 println("waited on timer at 750ms") 55 time.Sleep(time.Millisecond * 500) 56 }