Reply To: Display time spent on a trial to the participants

PennController for IBEX Forums Support Display time spent on a trial to the participants Reply To: Display time spent on a trial to the participants

#6963
amilam
Participant

Hi,

This is a sample code that illustrates what are you looking for:

PennController.ResetPrefix(null) // Keep here

newTrial(
    newVar("finishTime").set(v=>Date.now()) 
    ,
    newText("countDown", "0m0s").print()
    ,
    newTimer("updateCountdown",1000).callback( 
        newVar("difference")
            .set(getVar("finishTime")).set(v=>-v+Date.now())
        ,
                // Transform the Var element into an appropriately formatted string
                getVar("difference")
                    .set(v=>Math.trunc(v/60000)+"m"+Math.round((v/1000)%60)+"s")
                ,
                getText("countDown").text(getVar("difference"))
                ,
                // Relaunch the timer to update again in 1s
                getTimer("updateCountdown").start()
    ).start() // Don't forget to start the timer the initially
    ,
    newButton("Click me").print().wait()
    ,
    getVar("finishTime").set(v=>Date.now()) // This will effectively stop the countdown
    ,
    getTimer("updateCountdown").test.running()
        .success( newText("You clicked in time!").print() )
        .failure( newText("You were too late").print() )
    ,
    newButton("Finish").print().wait()
)

Hope it helps!

  • This reply was modified 2 years, 11 months ago by amilam.