waitOrCancelTrial(  duration )

Waits for one of two things:

      1. the given number of milliseconds to elapse, or…
      2. until the target end time of the session is reached and then cancels the current trial.  In this case, the trial is marked as cancelled by experimenter.

This function is designed to be used inside a trial (or a phase within a trial).



- The number of milliseconds to wait (and do nothing).

This argument is optional, and if not supplied will default to 10 milliseconds.

This function also uses the “targetDuration” property, which is usually set at the beginning of the codes.


The duration that the function waited if the trial was not cancelled.  If the trial was cancelled then the return value is irrelevant, because it is not possible to use it in this case.


This function is really useful when you have a phase within a trial that could run for a really long time, waiting for input (usually from an animal subject that might be distracted or not care about the screen).  In that case, rather than waiting indefinitely, you may want to design your phase to stop waiting after the session’s target duration has been reached.

Although the following example demonstrates this, a much more complete example is the DelayedMatchingToSample example found in the master collection of examples.

targetDuration = 1.5hours

waitOrCancel( 10ms )

print(“Waited for 10ms”)

waitOrCancel( 5sec )

print(“Waited for 5 seconds”)

waitOrCancel( 1min)

print(“Waited for 1 minute”)

waitOrCancel( 10:10pm )

print(“Waited until 10:10pm”)