Question
Problems with using Javascript to Record Amount of Audio Participant Listens To
Hello,
I am trying to create a survey in which I present an audio file for the participants to listen to. However, I would like to record how much of the total audio the participants actually choose to listen to (how much they choose to skip, etc.). While I am very new to Javascript, a friend helped me write code to record this information in an embedded data field called "totalTime." However, when I tried to enter this code onto the question, the embedded data (placed at the start of the survey) always reports a value of 0 despite the amount of audio is listened to. I am doing something wrong? Thanks for any help!
function roundNumber(number, digits){
var multiple = Math.pow(10, digits);
var rndedNum = Math.round(number * multiple) / multiple;
return rndedNum;
};
// variables to keep track of progress
var audioListened = 0;
var audioSkipped = 0;
var lastPosition = 0;
// initialize data to 0
Qualtrics.SurveyEngine.setEmbeddedData('totalTime', 0);
var audio_element = jQuery("audio.qmedia") // HTML audio element to track
// as the audio plays, we update the data with the total amount of time listened
audio_element.ontimeupdate = function(event) {
audioListened = event.currentTime - audioSkipped;
lastPosition = event.currentTime;
Qualtrics.SurveyEngine.setEmbeddedData('totalTime', roundNumber(audioListened, 2));
console.log(audioListened, audioSkipped);
};
// when participant skips time, we keep track of this to subtract from total time
audio_element.onseeked = function(event) {
audioSkipped += (event.currentTime - lastPosition); // adds fast-forwarding, subtracts rewinding from total
console.log(audioListened, audioSkipped);
};
_**
Enter your E-mail address. We'll send you an e-mail with instructions to reset your password.
