zoukankan      html  css  js  c++  java
  • timer定时器使用:

    Timer实际上是一种单一事件的定时器,即经过指定的时间后触发一个事件,这个事件通过其本身提供的channel进行通知。之所以叫单一事件,是因为Timer只执行一次就结束,这也是Timer与Ticker的最重要的区别之一。

    使用场景
    设定超时时间 如:
    func WaitChannel(conn <-chan string) bool {
      timer := time.NewTimer(1 * time.Second)
     
      select {
      case <- conn:
        timer.Stop()
        return true
      case <- timer.C: // 超时
        println("WaitChannel timeout!")
        return false
      }
    }
    延迟执行某个方法
     
    func DelayFunction() {
      timer := time.NewTimer(5 * time.Second)
     
      select {
      case <- timer.C:
        log.Println("Delayed 5s, start to do something.")
      }
    }
    • time.NewTimer(d)创建一个Timer;

    • timer.Stop()停掉当前Timer;

    • timer.Reset(d)重置当前Timer;

  • 相关阅读:
    Maria 与Ann的故事
    引语
    Preface
    Chapter 1 Foundation
    Roman to Integer
    Integer to Roman
    Container with most water
    palindrome number
    String to Integer (atoi)
    Reverse Integer
  • 原文地址:https://www.cnblogs.com/smallleiit/p/12482274.html
Copyright © 2011-2022 走看看