Waits for a value to become true or a function to return true.
Emulates built-in WinWait, KeyWait, etc. but supports any kind of check.
ret := wait(value, timeout := 0, interval := 100)value: The value continuously checked or the function whose return value is continuously checked.valueis being called (used as a function), if it is considered callable.
timeout– integer: Number of milliseconds to wait for at most.0(the default) will cause it to wait indefinitely.interval– integer: Number of milliseconds to wait before retrying.100is the default. Specifying0is valid and causes aSleep 0.ret: The return value.- If
timeoutwas reached,retis an empty string. - Otherwise (if
valuebecame true),retisvalueitself if it is not callable⁽¹⁾, or otherwise the return value of the last call tovalue.call().
- If
-
valueis considered callable when either of these conditions is met:type(value) == "Func"–valueis a Func Object.type(value) == "BoundFunc"–valueis a BoundFunc Object.value.hasMethod("Call")–valueis a Functor or any other object that implements aCallmethod.
These conditions are tested for in the order specified above, with short-circuit evaluation. If either test throws an exception (though only the last one is expected to do so),
valueis considered not callable. -
waitis able to throw an exception if the input arguments are invalid.timeoutandintervalmust both be a pure Integer and be0or greater. Any other exception is unintended and should be reported. Thank you!
; Example #1
; This will wait 1 second before setting x to true.
setTimer () => x := true, -1000
; This will consequently wait at least 1 second before continuing.
wait x; Example #2
; This will wait until the Spacebar is pressed,
; but will check for it much more frequently than KeyWait would.
wait () => getKeyState("Space"),, 10; Example #3
; This is exactly equivalent to the previous example.
wait func("getKeyState").bind("Space"),, 10; Example #4
; This will wait for Joystick #1 to be plugged in
; and display its name on the screen.
msgbox "Joystick '" wait(() => getKeyState("1JoyName")) "' has been plugged in!"