Every line of 'golang timer' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Go code is secure.
27 func (slf *Timer) SetupTimerDouble() { 28 slf.lasttime = time.Now().UnixNano() 29 slf.timeinterval *= 2 30 }
8 func main() { 9 10 timeout := time.NewTimer(5 * time.Second) 11 defer log.Println("Timed out!") 12 13 for { 14 select { 15 case <-timeout.C: 16 return 17 default: 18 } 19 } 20 21 }
44 func (cpu *CPU) timer(cycle int) { 45 for i := 0; i < cycle; i++ { 46 cpu.tick() 47 } 48 cpu.Sound.Buffer(4*cycle, cpu.boost) 49 }
14 func main() { 15 16 // Timers represent a single event in the future. You 17 // tell the timer how long you want to wait, and it 18 // provides a channel that will be notified at that 19 // time. This timer will wait 2 seconds. 20 timer1 := time.NewTimer(2 * time.Second) 21 22 // The `<-timer1.C` blocks on the timer's channel `C` 23 // until it sends a value indicating that the timer 24 // fired. 25 <-timer1.C 26 fmt.Println("Timer 1 fired") 27 28 // If you just wanted to wait, you could have used 29 // `time.Sleep`. One reason a timer may be useful is 30 // that you can cancel the timer before it fires. 31 // Here's an example of that. 32 timer2 := time.NewTimer(time.Second) 33 go func() { 34 <-timer2.C 35 fmt.Println("Timer 2 fired") 36 }() 37 stop2 := timer2.Stop() 38 if stop2 { 39 fmt.Println("Timer 2 stopped") 40 } 41 42 // Give the `timer2` enough time to fire, if it ever 43 // was going to, to show it is in fact stopped. 44 time.Sleep(2 * time.Second) 45 }
23 func SetInterval(call func(), sec int64) *time.Ticker { 24 ticker := time.NewTicker(time.Duration(sec) * time.Second) 25 go func() { 26 for { 27 select { 28 case <-ticker.C: 29 call() 30 } 31 } 32 }() 33 return ticker 34 }
11 func NewTimer() (t *Timer) { 12 t = &Timer{} 13 t.Start() 14 return 15 }
131 func (s *Strategy) startTimer() { 132 s.timer = time.NewTimer(s.timerFrequency) 133 134 eventLoop := func(ctx context.Context) { 135 for { 136 select { 137 case <-s.timer.C: 138 if !s.onTimer() { 139 // We did not publish. Reset the timer and try again later. 140 s.timer.Reset(s.timerFrequency) 141 } 142 case <-s.resetChan: 143 // Reset should be invoked only on stopped or expired timers with drained channels. 144 if !s.timer.Stop() { 145 select { 146 case <-s.timer.C: 147 default: 148 } 149 } 150 s.timer.Reset(s.timerFrequency) 151 case <-ctx.Done(): 152 // User requested to stop the timer. 153 s.timer.Stop() 154 return 155 } 156 } 157 } 158 159 // Start a go routine to listen to the timer. 160 _ = s.worker.Start(nil, eventLoop) 161 }
96 func (dm *DelayMessage) timeLoop() { 97 defer func() { 98 log.Println("时间遍历结束!") 99 }() 100 tick := time.NewTicker(time.Second) 101 for { 102 select { 103 case <-dm.timeClose: 104 return 105 case <-tick.C: 106 fmt.Println(time.Now().Format("2006-01-02 15:04:05")) 107 dm.curIndex = (dm.curIndex + 1) % cicleSectionNum 108 if dm.curIndex == 0 { 109 dm.cycleNum += 1 110 } 111 fmt.Println("当前循环时间", dm.cycleNum, dm.curIndex) 112 } 113 } 114 115 }
105 func CreateTimer(project *Project) *Timer { 106 timer := new(Timer) 107 timer.Project = project 108 timer.Start() 109 return timer 110 }
70 func NewTimer() *Timer { 71 t := &Timer{} 72 t.StartTimer() 73 return t 74 }