N
N
nikita_chiru2019-08-01 17:42:18
go
nikita_chiru, 2019-08-01 17:42:18

How to start the timer ( time.NewTimer) again several times?

Hello everyone, I want to restart the timer until the event is added
There was an idea, but it did not work.

func main() {
  var i = 0
  timer := time.NewTimer(time.Second * 1)
  select {
  case <-timer.C:
    if i != 3 {
      println(i)
      i++

      timer.Reset(time.Second * 1)
    }

  }
  println(i)
}

Response
$ go run timer.go
0
1

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexander Pavlyuk, 2019-08-01
@pav5000

You didn't add a loop here. Accordingly, your code runs once and exits.
What for to you in general here 3 times the timer to launch? Why not just start the timer at the desired end time?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question