Name
WaitTimer -- wait until a timer has reached a certain time (V2.0)
Synopsis
WaitTimer(id[, time, reset])
t = WaitTimer(table[, reset]) (V9.0)
Function
This function waits until the timer specified by id has been running for the time specified in the time argument. This time must be specified in milliseconds. If you omit the time argument or set it to -1, WaitTimer() will wait until the timer has reached its elapse threshold set using SetTimerElapse() or using StartTimer().

Before this function returns it will also reset the specified timer so that you can easily use this function in a loop. You can change this behaviour by setting the optional argument reset to False. In that case, the timer will not be reset.

Starting with Hollywood 9.0, there is an alternative way of using WaitTimer(). Instead of the identifier of a single timer, you can also pass a table containing multiple timer identifiers. In that case, WaitTimer() will wait until at least one of the timers from the list specified in the table argument has elapsed. Once that happens, WaitTimer() will return a table to you. That table will be a list containing the identifiers of all timers that have elapsed. If the reset argument is True, which is also the default, all elapsed timers will be reset by WaitTimer(). Note that the table you pass to WaitTimer() can also be empty. In that case, WaitTimer() will simply wait for a timer to elapse from all timers that are currently running.

WaitTimer() can be very useful to throttle the execution of loops so that they don't consume all of the CPU. For instance, if you have a loop that moves a sprite from the left to the right boundary of the display, you should add some kind of throttle because it doesn't make sense to update the screen more often than the monitor refreshes. This is very important. Even if the script runs at perfect speed without WaitTimer() you should not forget that there are faster machines than yours. Using WaitTimer() in your loops will make sure that your application runs at the same speed on every platform.

See Script timing for details.

Inputs
id
syntax 1: identifier of the timer to query
time
syntax 1, optional: time in milliseconds that the timer must have (defaults to timer's elapse threshold)
table
syntax 2: pass a table containing a list of timers here and WaitTimer() will return as soon as a timer from the list has elapsed (see above); if you pass an empty table, all running timers will be taken into account
reset
optional: specifies whether or not the timer shall be reset after WaitTimer() returns (defaults to True which means that the timer will be reset)
Results
t
syntax 2: a list of the timers that have elapsed; this will only be returned if you pass a table instead of a timer identifier to WaitTimer() (see above for details)
Example
StartTimer(1)
For k = 0 To 640
   DisplaySprite(1, k, 0)
   WaitTimer(1, 40)
Next
The above code scrolls sprite 1 from left to right. After each call to DisplaySprite(), WaitTimer() is used to ensure that we wait at least 40 milliseconds until the next DisplaySprite(). Thus, this loop will not be executed more than 25 times a second because 40 * 25 = 1000.

Show TOC