带有定时器的多个环路



所以我是AutoHotkey的新手,我在多循环计时器方面遇到了一些问题,它和第一个一样工作,但在第二个循环中,时间与我想要的不匹配。

所以基本上我想让循环运行5分钟,loopTwo应该是7秒后第一个出来的,然后2秒后我想让loopOne在loopOne中被调用。两次按下之间有1.2秒的延迟,第一次它正常工作,但后来时间开始改变,所有东西都加入了一个混乱的

F1::
If (loopOne = True) 
{
SetTimer loopTwo, Off
TwoSwitch  := False
SetTimer loopOne, Off
OneSwitch := False
} else {
TheTwoTime := 0
SetTimer loopTwo, 7000 ;run every 7s
TwoSwitch := True
TheOneTime := 0
SetTimer loopOne, 9000 ;run every 9s
OneSwitch := True
}
return

loopOne:
Send, 1
Sleep, 1200
Send, 1
TheOneTime ++
If TheOneTime >= 300 ;run for 5 minutes
{
SetTimer loopOne, Off
OneSwitch := False
}
return
loopTwo:
Send, 2
Sleep, 2000
TheTwoTime ++
If TheOneTime >= 300 ;run for 5 minutes
{
SetTimer loopTwo, Off
TwoSwitch := False
}
return

我想这就是你想要做的。假设我正确理解你的事情,我认为不需要两个定时器。

还抛弃了传统标签,改用SendInput,因为它是首选的更快、更可靠的发送模式
除了使用toggle:=!toggle切换之外,应该是一个非常直接的脚本。如果你不能理解,你可以在这里看到我的一个旧答案
还要注意计时器中负周期的用法,这是一件非常有用的事情。

F1::
if (toggle:=!toggle)
{
SetTimer, MyCoolLoop, 7000 ;7sec period
SetTimer, StopLooping, -300000 ;negative period, run ONCE after 5mins
}
else
SetTimer, MyCoolLoop, Off
return
MyCoolLoop()
{
;number 2 gets sent (every 7secs)
;2secs after this, number 1 gets sent
;1.2secs after this, number 1 gets sent again
;3.8secs after this, we start from the beginning
SendInput, 2
Sleep, 2000
SendInput, 1
Sleep, 1200
SendInput, 1
}
StopLooping()
{
SetTimer, MyCoolLoop, Off
}

最新更新