Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Supported by

Get worker ID at the beginning of the experiment

edited August 2021 in JATOS

Hi there,

Sorry for posting this simple question here. I have a jsPsych experiment that is planned to host on JATOS, using a General Multiple Worker link for distribution. From my understanding, a worker ID in numerical order will be assigned to the participant once she/he click the link. I wonder how I can get the value of worker ID in my JavaScript code? I tried many ways to do so (e.g., PartID = jatos.workerId) but I just couldn't get the number. Could you please tell me how I should do that? Thanks a lot!

Comments

  • Hi!

    You have to wait for jatos.js to be initialized. Only then you have access to these IDs:

    jatos.onLoad(function() {
        var PartID = jatos.workerId;
    });
    

    Best,

    Kristian

  • edited August 2021

    Dear Kristian,

    Thank you so much for your answer! I tried your way but it didn't seem to work. Basically I want to get the worker ID in order to assign experimental condition to the participant. The code of condition assignment is as follow.

    function assign(x) {//to assign priming condition
        if (x % 4 == 1) {
            return 'list1';
        }
        if (x % 4 == 2) {
            return 'list2';
        }
        if (x % 4 == 3) {
            return 'list3';
        }
        if (x % 4 == 0) {
            return 'list4';
        }
    };
    var condition_assignment = assign(Part_ID);
    

    And at the end of the script, I defined Part_ID in jatos.onLoad. The code is as follow:

    function startExp() {
        jatos.onLoad(function () {
            var Part_ID = jatos.workerId;
            jsPsych.init({
                timeline: timeline,
                on_interaction_data_update: function (data) {
                    // get the main trial data
                    var trial = jsPsych.currentTrial();
                    trial.data.screen_focus = data.event;
                },
                preload_audio: audio,
                preload_images: image,
                max_load_time: 300000,
                use_webaudio: false,
                on_finish: function () {
                    var filedata = jsPsych.data.get().csv();
                    var filename = Part_ID + "_" + DATE + ".csv";
                    jatos.submitResultData(filedata)
                    jatos.uploadResultFile(filedata, filename)
                        .then(function () {
                            alert("Data upload successful! You will be directed to the Prolific page");
                            jatos.startComponent(3311);
                        }).catch(function () {
                            alert("Data upload unsuccessful. Please download the result data to your local hard drive and contact the experimenter chi.zhang.jarvis@outlook.com");
                            downloadCSV(filedata, filename);
                            jatos.startComponent(3311);
                        })
                }
            })
        }
        )
    }
    

    Would you please tell me where my code went wrong? Thank you so much!

    Best regards,

    Chi

  • Hi Chi!

    I took the liberty and formated your code to make it more readable.

    Your startExp function looks fine. When do you call assign function? Is it maybe before startExp? Then Part_ID wouldn't be initialized yet.

    Best

    Kristian

  • Hi Kristian,

    Thanks for your suggestion! Now I put startExp function before condition assignment. But still, the console told me that Part_ID is not defined when condition assignment is called.

    Best regards,

    Chi

  • It is a JavaScript issue. The scope of your Part_ID is limited to startExp function. You have to declare it as global variable first and then you can use it outside of startExp:

    var Part_ID; // global variable declaration
    
    startExp();
    var condition_assignment = assign(Part_ID);
    
    function startExp() {
        jatos.onLoad(function () {
            Part_ID = jatos.workerId;
            ...
        }
        )
    }
    
    function assign(x) {
        ...
    }
    

    You probably don't need startExp and Part_ID at all and could just:

    var condition_assignment;
    
    jatos.onLoad(function () {
        condition_assignment = assign(jatos.workerId);
            ...
    });
    
    function assign(x) {
        ...
    }
    

    Best,

    Kristian

  • Dear Kristian,

    Thanks again for the help! Unfortunately, I still could not make either one of the solution work. The Part_ID did not seems to be defined during the condition assignment. Nevertheless, the value of jatos.workerID did pass to Part_ID when the variable filenamewas defined (it was embedded in the on_finish parameter of jspsych.init). So the code did work, but only at the end of the experiment. I wonder why I could not pass the value workerID to Part_ID during the experiment? Could there be further issue with the order of my code?

    Best regards,

    Chi

  • If you share your experiment with me I can have a look at your code. You can send me your experiments jzip if possible.

    Best,

    Kristian

  • Hi Chi,

    The easiest way to fix your experiment is to encompass your whole experiment, everything in startExp function, in jatos.onLoad and have the initialization of Part_ID in the beginning:

    function startExp() {
      jatos.onLoad(function () {
        var Part_ID = jatos.workerId;
        var condition_assignment = assign(Part_ID);
        var focus = instruct(Part_ID);
        ...
    
        jsPsych.init({
            ...
        });
      }); // closes jatos.onLoad
    }
    

    Another thing I noticed in your code is that you probably don't want to use jatos.startComponent but jatos.startComponentByPos. jatos.startComponent uses the component ID as the parameter which changes from JATOS instance to JATOS instance. But the position of your component within your study stays always the same. You can always try out your study on cortex.jatos.org or jatos.mindprobe.org.

    Best,

    Kristian

  • Dear Kristian,

    Now I can get the value of workerID when assigning conditions. That works perfectly. jatos.startComponentByPos makes a lot of sense to me and is much convenient. Thank you so much for the help!

    Best regards,

    Chi

Sign In or Register to comment.