coglabuzh / online-exps

Unleash the full potential of JavaScript and supercharge your web experiments with this library for experimental programming - based on jsPsych - work in progress! 🎯
2 stars 0 forks source link

Enhance and merge basic functions #13

Closed chenyu-psy closed 11 months ago

chenyu-psy commented 11 months ago

Hi Ajit,

I have uploaded my functions; however, some of them do not work well. The main issue is that some of the functions rely on a global variable from the other script. Could you help me check if it is possible to undo the reliance on global variables (e.g., countdownTimer function)?

In addition, I noticed that some of my functions are doing the same work as yours. Could you try merging them?

ajit283 commented 11 months ago

working on this

ajit283 commented 11 months ago

Hi Chenyu,

I saw that some of your functions were importing a jsPsych object. This is unique to each experiment and therefore cannot be imported statically. I added it as a type-checked argument to each function that needs it. I also added the dependency on sweetalert2.

However, I did not encounter any additional global variables. In countdownTimer, timeSwitch is an argument and doesn't seem to depend on any global variable. I probably misunderstood what you mean by global variables.

The functionality of my two functions is covered by some of your functions as well. However, I am reluctant to remove mine as this would break compatibility of all experiments currently using the library. We can discuss in the next meeting how to deal with this.

chenyu-psy commented 11 months ago

Hi Ajit,

Thank you for your work and I am sorry for the confusion.

Yes, timeSwitch is an argument used to input the global variable RUN_TIMER into the function. In my experiment, I used trial_start_screen to show participants how much time do they still have to rest. Participants are allowed to skip the screen if they want to continue. The problem is that when participants skip the screen, the countdown timer does not disappear. It continually displays until the countdown equals 0. To avoid this, I use the global variable RUN_TIMER to check whether the current screen has been finished or not. If it has been finished, I use clearInterval(intervalId) to clear the timer.

I wonder if there is a way that I don't need to use RUN_TIMER variable, and the timer could stop automatically when participants switch to the next screen. In that case, this function would be more easy to use.

ajit283 commented 11 months ago

Hi Chenyu,

Thanks for your clarification - the user of the function should not need to remember to add anything to the on_finish function. One possible approach to avoid this would be to pass the jsPsych object into the function and then modify the on_finish function inside the plugin by calling jsPsych.getCurrentTrial().on_finish = () => {...}. However, one has to ensure that anything that is already inside the on_finish function still gets executed. I will try this approach.

chenyu-psy commented 11 months ago

Hi Ajit,

Thank you for your reply. If this task will take more than an hour, feel free to leave it. It's not a big issue.

ajit283 commented 11 months ago

Hi Chenyu, I have implemented the function as described, the new version is now in the dev branch

chenyu-psy commented 11 months ago

Excellent!

Just want to clarify, Do I still need to create an empty function in on_finish? The code like this:

export const trial_start_screen = {
  type: htmlButtonResponse,
  stimulus: `<div class="fb-text">
    <p>The next trial will start in <span id="clock" style="color:red">10</span> seconds.</p>
    <p>Click on the "continue" button to start directly.</p>
    <br>
    <br>
  </div>`,
  choices: ["Continue"], // The only valid key response is the space bar.
  trial_duration: TIMING.START, // Time to wait before automatically proceeding with the next trial.
  post_trial_gap: 1000, // forced inter-trial interval after participant's response.
  on_load: function () {
    let time = convertTime(TIMING.START, "ms", "s");
    //@ts-ignore
    countDownTimer(time, "clock", jsPsych);
  },
  on_finish: function () {
    // varSystem.RUN_TIMER = false;
  },
};

I found that there will be an issue if I delete the on_finish function: image

ajit283 commented 11 months ago

Dear Chenyu, thanks for making me aware. This should be fixed in ver. 1.1.3, the on_finish function is not mandatory anymore.

chenyu-psy commented 11 months ago

Thank you, Ajit! I have tested most of the functions, and they work well in my experiment.