Timer
element
description
Represents a timer.
syntax
newTimer(ELEMENT_NAME: string, TIMER_LENGTH: int)
-
ELEMENT_NAME
: The name of the newly-created element. -
TIMER_LENGTH
: The length of the timer, in milliseconds.
since
beta 0.3example(s)
@newText("pleasewait", "Please wait 1s.")
@ .print()
@,
$newTimer("wait", 1000)
$ .start()
$ .wait()
@,
@getText("pleasewait")
@ .remove()
+ The code above adds the text Please wait 1s to the screen, starts a 1000ms timer and waits until it is done before removing the text. Action commands
timer.callback
syntax
.callback()
description
Warning: until PennController beta 0.2, this command has a bug that freezes the script
example
click to expand
@
@newText("start", "Ready... go!")
@ .print()
@,
@newButton("click", "Click me!")
@ .print()
@,
@newTimer("hurry", 1000)
$ .callback( getButton("click").remove() )
@ .start()
@,
@getButton("click")
@ .wait()
@,
@newText("success", "You're quick!")
@ .print()
- Adds a line of text saying Ready… go! to the screen above a button reading Click me!, then starts a 1s timer and waits for a click on the button. After 1s, the button disappears. The text You’re quick! appears on the page after a click on the button. If the button was not clicked within 1s, the text does not show up and the script waits for a click forever (since the button no longer can be clicked). If the button was clicked, then the button’s
wait
command is validated and the text shows up (the button will still disappear after one second, as specified in the callback).
timer.log
syntax
.log()
description
Will add a line to the results file each time the timer starts and each time it ends.
example
click to expand
@
@newText("pleasewait", "Please wait 1s.")
@ .print()
@,
@newTimer("wait", 1000)
$ .log()
@ .start()
@ .wait()
@,
@newButton("continue", "Now click here to continue.")
@ .print()
@ .wait()
- Adds the text Please wait 1s to the screen, starts a 1000ms timer and wait until it is done before adding a button to the page. Two lines will be added to the results file, reporting the timestamps at the start and at the end of running the timer (should be spaced apart by about 1000ms).
timer.pause
syntax
.pause()
description
Pauses the timer.
example
click to expand
@newText("pleasewait", "Please wait 1s.")
@ .print()
@,
@newTimer("wait", 1000)
@ .start()
@,
@getTimer("wait")
$ .pause()
- The code above adds the text Please wait 1s to the screen, starts a 1000ms timer and then pauses it.
timer.resume
syntax
.resume()
description
Resumes the timer.
example
click to expand
@newText("pleasewait", "Please wait 1s.")
@ .print()
@,
@newTimer("wait", 1000)
@ .start()
@,
@getTimer("wait")
@ .pause()
@getTimer("wait")
$ .resume()
- The code above adds the text Please wait 1s to the screen, starts a 1000ms timer and then pauses it, and then resumes it again.
timer.set
syntax
.set()
description
Re(set) timer.
example
click to expand
- The set command lets user (re)set the duration of the timer element independently of its creation.
@newTimer("time"),
@newScale("mode", "easy", "hard")
@ .button().print().wait().remove()
@ .test.selected("easy")
$ .success( getTimer("time").set(2000) )
$ .failure( getTimer("time").set(1000) )
@,
@newScale("answer", "Answer A", "Answer B")
@ .button().print()
@,
@getTimer("time").start().wait()
@ .remove()
- The code above gives 2s to answer in the easy mode, but only 1s in the hard mode.
timer.start
syntax
.start()
description
Starts the timer.
example
click to expand
@newText("pleasewait", "Please wait 1s.")
@ .print()
@,
@newTimer("wait", 1000)
$ .start()
@ .wait()
@,
@getText("pleasewait")
@ .remove()
- The code above adds the text Please wait 1s to the screen, starts a 1000ms timer and wait until it is done before removing the text.
timer.stop
syntax
.stop()
description
Stops the timer early. Nothing happens if the timer has already elapsed.
example
click to expand
@
@newText("instructions", "Wait 3s or press Space")
@ .print()
@,
@newTimer("delay", 3000)
@ .start()
@,
@newKey("stop early", " ")
$ .callback( getTimer("delay").stop() )
@,
@getTimer("delay")
@ .wait()
-
Will wait 3 seconds before proceeding, or will proceed if the space bar is pressed in the meantime.
timer.wait
syntax
.wait()
description
Waits until the timer has finished running before evaluating and executing the next commands.
example
click to expand
@
@newText("instructions", "Please press the spacebar in less than a second")
@ .print()
@,
@newKey("spacebar", " ")
@,
@newTimer("hurry", 1000)
@ .start()
$ .wait()
@,
@getKey("spacebar")
@ .test.pressed(" ")
@ .success( newText("success", "Good job!").print() )
@ .failure( newText("failure", "Too slow...").print() )
- Adds a line of text to the page, starts listening for any press on the spacebar, waits 1 second (1000ms) and tests whether the key was pressed: if it was, adds Good job! to the screen, adds Too slow… otherwise.
Test commands
timer.test.ended
syntax
.test.ended()
description
Tests whether the timer has ended.
example
click to expand
@
@newText("start", "Ready... go!")
@ .print()
@,
@newTimer("hurry", 1000)
@ .start()
@,
@newButton("click", "Click me!")
@ .print()
@ .wait()
@,
@getTimer("hurry")
$ .test.ended()
@ .success( newText("slow", "Too slow...").print() )
@ .failure( newText("fast", "Good job!").print() )
- Adds a line of text saying Ready… go! to the screen, starts a 1s timer, adds a button reading Click me!, and waits for a click on the button. If the timer has ended by the time the button is clicked, the text Too slow… is added to the screen, otherwise if the button is clicked within 1s, Good job! appears below it.
timer.test.running
syntax
.test.running()
description
Tests whether the timer is currently running.
example
click to expand
@
@newTooltip("not running", "The timer is not running")
@,
@newText("on", "[on]")
@ .before( newText("off", "[off]") )
@ .print()
@,
@newSelector("switch")
@ .add( getText("on") , getText("off") )
@,
@newTimer("quick", 500)
@ .callback( getSelector("switch").select(getText("off")) )
@,
@getSelector("switch")
@ .callback(
@ getSelector("switch").test.selected( getText("on") )
@ .success( getTimer("quick").start() )
@ )
@,
@newButton("target", "Click here within 500ms of start")
@ .print()
@ .wait(
$ getTimer("quick")
$ .test.running()
$ .failure( getTooltip("not running").print("target") )
@ )
- Shows off and on on a single line above a button reading Click here within 500ms of start. When on is clicked, the 500ms timer starts. If the button is clicked within 500ms, then the
running
test is successful and the click is validated. If the target button is clicked before the timer has started (i.e. before on was clicked) or after the timer has ended, therunning
test fails and thewait
command does not release the execution of the script: the click is not validated and a new attempt at a on-target click sequence within 500ms is necessary.