Function element

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


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


Note: no pre-comma linebreaks in code below

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

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.


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


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