gignu
gignu

Reputation: 2507

AutoHotkey - Running Loop for a Limited Amount of Time

I'm trying to run my loop for two seconds. Within those two seconds, if I click left a message box gets activated, telling me that I've clicked left. If the 2 seconds are up another message box is supposed to appear, telling me that I've been waiting enough. However, after 2 seconds nothing happens ;(

    :*:tcc::
    start := A_TickCount
    totalTime := stop - start
    Loop {
        stop := A_TickCount     
        if (totalTime > 2000)
            {
            MsgBox, enough waiting!
            return
            }   
        else if GetKeyState("LButton")
            {
            MsgBox, you clicked left
            return
            }
    }

Upvotes: 0

Views: 104

Answers (1)

Relax
Relax

Reputation: 10636

The variable "totalTime" has to be created within the loop, every time the loop stops:

:*:tcc::
    start := A_TickCount    
    Loop {
        stop := A_TickCount  
        totalTime := stop - start   
        if (totalTime > 2000)
        {
            MsgBox, enough waiting!
            return
        }   
        else if GetKeyState("LButton")
        {
            MsgBox, you clicked left
            return
        }
    }
return

Upvotes: 1

Related Questions