Reply To: Refreshing canvas

PennController for IBEX Forums Support Refreshing canvas Reply To: Refreshing canvas

#6997
Jeremy
Keymaster

Hi Rafael,

The reasoning is the same here: elements are parts of trials, so when their trial ends, they cease to exist. The only exception is the Var element, which can be made global so you can access and manipulate its value across trials

If you need to keep track of how much time has passed since the beginning of the experiment, you can set a javascript variable to Date.now() and check it on every trial, for example inside a Header. Here’s an example that will print a 2min timer in the top-left corner of the page, and clear the screen and print a “Timed out!” message when there’s not time left:

ALLOTEDTIME = 2*60*1000
startTime = Date.now()
toMinSec = v=>Math.floor((ALLOTEDTIME-v)/60000)+":"+Math.floor(((ALLOTEDTIME-v)/1000)%60)

Header(
    newText("timer", "").print("left at 2em", "top at 2em")
    ,
    newVar("time elapsed")
        .set(v=>Date.now()-startTime)
        .test.is(v=>v>ALLOTEDTIME).success( end() )
    ,
    newTimer("timeout", 500)
        .callback(
            getVar("time elapsed")
                .set(v=>Date.now()-startTime)
                .test.is(v=>v>ALLOTEDTIME)
                .success(
                    clear()
                    ,
                    newText("Time out!").print()
                    ,
                    newButton().wait()   
                )
                .failure( 
                    getVar("time elapsed").set(toMinSec)
                    ,
                    getText("timer").text(getVar("time elapsed"))
                    ,
                    getTimer("timeout").start() 
                )
        )
        .start()
)

Jeremy