dongzong3053 2018-06-25 10:41
浏览 62

无法停止计时器

I have defined few tickers and when I try to stop them, I see below

undefined: ticker in ticker.Stop

undefined: Q30sticker in Q30sticker.Stop

Code sample:

if activenode() {
                    cipaflage = true
                    break

                } else {
                    if cipaflage == true {
                        defer ticker.Stop()
                        defer Q30sticker.Stop()
                        cipaflage = false
                        }
                    continue
                }

And I have declared the ticker as below

ticker := time.NewTicker(59 * time.Second)
Q30sticker := time.NewTicker(time.Second * 30).C
  • 写回答

1条回答 默认 最新

  • dongran1779 2018-06-25 12:05
    关注

    As per the docs here

    Ticker has an exported field C.

    type Ticker struct {
            C <-chan Time // The channel on which the ticks are delivered.
            // contains filtered or unexported fields
    }
    

    You can receive the "ticks", or heartbeats on the C channel, but if you are doing this:

    Q30sticker := time.NewTicker(time.Second * 30).C
    

    then Q30sticker holds a reference to the channel C and not the ticker type. It is the time.Ticker struct that defines the Stop method.

    Update your variable to hold the ticker type (by removing .C):

    Q30sticker := time.NewTicker(time.Second * 30)
    

    And where you receive from the channel, use the . dot syntax to access the channel.

    <-Q30sticker.C
    
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)