【问题标题】:Why doesn't timer treat global variable correctly?为什么计时器不能正确处理全局变量?
【发布时间】:2020-06-04 01:52:42
【问题描述】:

这会不断发送t:

myVar := true

SetTimer, myLoop, 0

myLoop(){
    if (true) {
      Send, {t down}
    }
}

#Persistent

但这无济于事:

myVar := true

SetTimer, myLoop, 0

myLoop(){
    if (myVar) {
      Send, {t down}
    }
}

#Persistent

为什么? myVartrue 都评估为 true 所以我不明白为什么它不同。

【问题讨论】:

    标签: autohotkey


    【解决方案1】:

    myVar := true 应该是:

    global myVar := true
    

    因为必须明确声明 global

    【讨论】:

    • 为什么会这样?
    • 必须明确声明全局变量。
    • 你没有用这个声明一个全局变量,你声明了一个 super-global 变量,这是不好的做法,应该只在一些快速的小不重要的脚本中使用方便。从文档here 中了解全局变量的用法。如果你想看更长的文字解释,我有一篇关于它的旧帖子here
    猜你喜欢
    • 2023-03-25
    • 2015-07-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-21
    • 2015-05-17
    相关资源
    最近更新 更多