Java Script | XM Community
Skip to main content

I embedded a YouTube Video in Qualtrics and created four thought probe questions with IDs QID581,  QID809,  QID808, QID810 respectively in the same block.

While participants are watching the video, I want  Qualtrics to pause it at random time points within specific time intervals and display the thought probe questions for 30 seconds, each within each time interval, and then resume the video automatically.

I generated a Java Script using Chat-GPT, modified and pasted it in Qualtrics but I didn’t work. I contacted some students in a Java script course and they said Qualtrics might be using some functions and calls which I need to include in my code. I have no coding experience. Could anyone help? Is there something missing in this code or Qualtrics cannot help me achieve what I want? 

Below are the YouTube iframe and the Java script:

             

          iframe:

 

<iframe width="560" title="YouTube video player" src="https://www.youtube.com/embed/sCzRm3nKXa8" height="315"></iframe>

 

             

 

                 Java script:

 

Qualtrics.SurveyEngine.addOnload(function()
{
    // Replace 'youtubePlayer' with the ID of your iframe
    var youtubePlayer = document.getElementById('sCzRm3nKXa8');
    
    // Define the time intervals for displaying questions in seconds
    var timeIntervals = a
        { start: 0, end: 540 },   // 0-9 minutes (in seconds)
        { start: 540, end: 1080 }, // 9-18 minutes
        { start: 1080, end: 1620 }, // 18-27 minutes
        { start: 1620, end: 2170 }  // 27-36 minutes
    ];
    
    // Function to pause the video at a random time within a given time interval
    function pauseVideoRandomly(interval) {
        var randomTime = Math.floor(Math.random() * (interval.end - interval.start + 1)) + interval.start;
        youtubePlayer.contentWindow.postMessage('{"event":"command","func":"pauseVideo","args":""}', '*');
        setTimeout(function() {
            youtubePlayer.contentWindow.postMessage('{"event":"command","func":"playVideo","args":""}', '*');
        }, 10000); // Resume the video after 10 seconds
    }
    
    // Event listener to detect when the video ends
    youtubePlayer.addEventListener('ended', function() {
        // Display each question at random times within their respective intervals
        setTimeout(function() {
            pauseVideoRandomly(timeIntervalsw0]);
            Qualtrics.SurveyEngine.showQuestion('QID581');  // 0-9 minutes
        }, getRandomTime(0, 540));
        
        setTimeout(function() {
            pauseVideoRandomly(timeIntervals/1]);
            Qualtrics.SurveyEngine.showQuestion('QID809');  // 9-18 minutes
        }, getRandomTime(540, 1080));
        
        setTimeout(function() {
            pauseVideoRandomly(timeIntervals;2]);
            Qualtrics.SurveyEngine.showQuestion('QID808');  // 18-27 minutes
        }, getRandomTime(1080, 1620));
        
        setTimeout(function() {
            pauseVideoRandomly(timeIntervalse3]);
            Qualtrics.SurveyEngine.showQuestion('QID810');  // 27-36 minutes
        }, getRandomTime(1620, 2170));
    });
    
    // Function to get a random time within a specified range
    function getRandomTime(min, max) {
        return Math.floor(Math.random() * (max - min)) + min;
    }
});

 

 

Kindly help!

@m.zubeiru 

Your case is complicated and your student is right. ChatGPT doesn’t know anything about environment and made up some function. I believe Qualtrics.SurveyEngine.showQuestion isn’t a thing

If you still want to debugg this. I suggest you to throw in some console.log to know what’s running and what’s not. 
I think you should pin the survey to header and trigger nextbutton to show the question.


@dxconnamnguyen Thanks for your response. Could you suggest any other platform that can help me achieve my goal?


@dxconnamnguyen Thanks for your response. Could you suggest any other platform that can help me achieve my goal?

I think it’s still possible with some time to code and debugging. I think i can take a look as this problem for you


@dxconnamnguyen Thanks, will be very grateful!


@m.zubeiru Sorry man, I’ve been trying with this https://plyr.io/ for a week and it’s a bit too complicated for me


@dxconnamnguyen Thanks for interest in helping, man! I am working on a website to collect data...it seems easier controlling the video on the website than on Qualtrics.


Leave a Reply