# Timing experiments

**URL:** <https://discourse.psychopy.org/t/timing-experiments/8516>\
**Category:** Online experiments\
**Created:** [July 15, 2019, 10:20am UTC](https://discourse.psychopy.org/t/timing-experiments/8516 "2019-07-15T10:20:19Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![rob-linguistics13](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/rob-linguistics13/32/5558_2.png) [@rob-linguistics13](https://discourse.psychopy.org/u/rob-linguistics13)\
**Post date:** [July 15, 2019, 10:20am UTC](https://discourse.psychopy.org/t/timing-experiments/8516/1 "2019-07-15T10:20:20Z")

</div>

I was wondering if could help me figure out what is the best way to record the _actual_ time each subject took to take an online experiment.

A way I was thinking about, for example, was to look at the time of creation and modification of the data files, but I do not know much about how datafiles are created in Pavlovia. It seems that the server stores the data and put into a csv file only when the experiment is over (i.e., when the pop-up window shows up).

Another way would be to implement an actual `Clock` object, make it start at the very beginning of the experiment (e.g., right after the diag box) and stop after the very last trial (i.e., right before the pop-up window shows up) and print that value in the csv file with `psychoJS.experiment.addData()`.

Thoughts? Thanks!

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [July 15, 2019, 11:27am UTC](https://discourse.psychopy.org/t/timing-experiments/8516/2 "2019-07-15T11:27:11Z")

</div>

Hi @rob-linguistics13, PsychoPy and PsychoJS both have a built-in clock called `globalClock`, which is created at the beginning of the experiment. All you need to do is make a call to get the time of that clock at the end of your experiment:

```javascript
psychoJS.experiment.addData("globalClock", globalClock.getTime())

```

---

<div class="post-metadata">

**Author:** ![rob-linguistics13](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/rob-linguistics13/32/5558_2.png) [@rob-linguistics13](https://discourse.psychopy.org/u/rob-linguistics13)\
**Post date:** [July 15, 2019, 7:20pm UTC](https://discourse.psychopy.org/t/timing-experiments/8516/3 "2019-07-15T19:20:30Z")

</div>

Sorry, @dvbridges - where should I put that string of code?

```auto
function endLoopIteration(thisScheduler, thisTrial) {
  // ------Prepare for next entry------
  return function () {
    // ------Check if user ended loop early------
    if (currentLoop.finished) {
      thisScheduler.stop();
    } else if (typeof thisTrial === 'undefined' || !('isTrials' in thisTrial) || thisTrial.isTrials) {
      psychoJS.experiment.nextEntry();
    }
  return Scheduler.Event.NEXT;
  };
}

function importConditions(loop) {
  const trialIndex = loop.getTrialIndex();
  return function () {
    loop.setTrialIndex(trialIndex);
    psychoJS.importAttributes(loop.getCurrentTrial());
    return Scheduler.Event.NEXT;
    };
}

function quitPsychoJS(message, isCompleted) {
  psychoJS.window.close();
  psychoJS.quit({message: 'The experiment is now complete. Thank you for your participation!\nPlease copy-and-paste the following password into Mechanical Turk so that we can confirm your participation: ASYMPTOTE.\nOnce you do that, you can close this page.', isCompleted: isCompleted});

  return Scheduler.Event.QUIT;
}

```

I tried to put between the two functions above, but an error popped up; I tried to put it inside the `quitPsychoJS` function, but the experiment would go through without storing the variable.

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [July 17, 2019, 4:10pm UTC](https://discourse.psychopy.org/t/timing-experiments/8516/4 "2019-07-17T16:10:11Z")

</div>

I would put it in the last occuring `RoutineEnd` function in your task, for example if you had an end screen thanking the participant etc.

---

<div class="post-metadata">

**Author:** ![rob-linguistics13](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/rob-linguistics13/32/5558_2.png) [@rob-linguistics13](https://discourse.psychopy.org/u/rob-linguistics13)\
**Post date:** [July 17, 2019, 4:28pm UTC](https://discourse.psychopy.org/t/timing-experiments/8516/5 "2019-07-17T16:28:16Z")

</div>

There is no `End` routine. The diag box pops up as soon as all the items are presented. Any alternative solution? Maybe some sort of `if` statement on the last routine, e.g.:

```auto
#End Routine

if (loop.nRemaning==0){
   psychoJS.experiment.addData("globalClock", globalClock.getTime());
}

```

---

<div class="post-metadata">

**Author:** ![dvbridges](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/dvbridges/32/1918_2.png) [@dvbridges](https://discourse.psychopy.org/u/dvbridges)\
**Post date:** [July 17, 2019, 4:45pm UTC](https://discourse.psychopy.org/t/timing-experiments/8516/6 "2019-07-17T16:45:31Z")

</div>

Ah i see, I thought you were editing the JS. No need for an if statement. You could add it to the End Routine tab of a code component in the last routine and just take the last entry in your datafile as the end exp time.

---

<div class="post-metadata">

**Author:** ![rob-linguistics13](https://sea2.discourse-cdn.com/flex002/user_avatar/discourse.psychopy.org/rob-linguistics13/32/5558_2.png) [@rob-linguistics13](https://discourse.psychopy.org/u/rob-linguistics13)\
**Post date:** [July 17, 2019, 5:05pm UTC](https://discourse.psychopy.org/t/timing-experiments/8516/7 "2019-07-17T17:05:15Z")

</div>

> [@dvbridges](#):
>
> I thought you were editing the JS.

Sorry @dvbridges I misunderstood what you were saying. Yes, I am editing the JS script. The thing is that my last routine is within a loop. I fear that if I don’t include an if-statement, the clock variable will not get the time at the end of the experiment (actually, it will in the end but I think it will get update at each repeat of the loop which might slow down the whole presentation).

> [@dvbridges](#):
>
> just take the last entry in your datafile as the end exp time.

What do you mean by this exactly?
