Function element

Function elements let you execute javascript functions at a given point during the trial.

Creation:

newFunction("myFunction", () => console.log("hello world") )

Example:

Note: no pre-comma linebreaks in code below

[js try=”true”]
newFunction(“setKeyTime”, function(){ this.keyTime = Date.now(); }),
newFunction(“keyTimerDelay”, function(){ return Date.now() – this.keyTime > 10; }),
newButton(“Start”, “Start”)
.print()
.wait(),
newText(“instrutions”, “Quick, you have one second to press the T key!”)
.print(),
newTimer(“quick”, 1010)
.start(),
newKey(“t”, “T”)
.wait(),
getFunction(“setKeyTime”)
.call(),
getTimer(“quick”)
.wait(“first”),
getFunction(“keyTimerDelay”)
.test.is(true)
.success(
newText(“success”, “Good job!”)
.print()
)
.failure(
newText(“failure”, “Too slow…”)
.print()
)
[/js]

The code above adds a button to the page; after it is clicked, instructions to press T in less than 1 second appears on the screen, and a 1010ms timer starts running. The script waits for the timer to elapse after the T key is pressed: if this takes more than 10ms, it means the there was more than 10ms left to the timer after the key was pressed, and Good job! appears on the screen.

The code above manipulates two function elements, named setKeyTime and keyTimerDelay. Both elements are created at the beginning of the trial. The function of setKeyTime is executed only after the T key was pressed: it sets an trial-internal variable (this.keyTime) to the current timestamp. The function of keyTimerDelay is executed after the timer has elapsed (and the key was pressed): its return value is tested, and if it is true (i.e. the delay between the current time and the time when the key was pressed is greater than 10ms) then the success command is executed, otherwise the failure command is executed.

Commands

[yadawiki-list category=”NonTest command,Function element”]

Tests

[yadawiki-list category=”Test command,Function element”]