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.value
is 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.100
is the default. Specifying0
is valid and causes aSleep 0
.ret
: The return value.- If
timeout
was reached,ret
is an empty string. - Otherwise (if
value
became true),ret
isvalue
itself if it is not callable⁽¹⁾, or otherwise the return value of the last call tovalue.call()
.
- If
-
value
is considered callable when either of these conditions is met:type(value) == "Func"
–value
is a Func Object.type(value) == "BoundFunc"
–value
is a BoundFunc Object.value.hasMethod("Call")
–value
is a Functor or any other object that implements aCall
method.
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),
value
is considered not callable. -
wait
is able to throw an exception if the input arguments are invalid.timeout
andinterval
must both be a pure Integer and be0
or 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!"