Display elapsed time when click a button | XM Community
Skip to main content

Hi, I want to display the elapsed time when the participant clicks some button. Here is what I did:
in the html code, i create one button and the message :





   
  
 



in the java code,
Qualtrics.SurveyEngine.addOnload(function() {
var starttime = new Date().getTime(); // start time is the page on load time
});
Qualtrics.SurveyEngine.addOnReady(function()
{
  /*Place your JavaScript here to run when the page is fully displayed*/
jQuery('#TimeButton').on('click', () => {
var pastWT = new Date().getTime()-starttime; // elapsed time is the button clicked time - start time
Qualtrics.SurveyEngine.setEmbeddedData('elapsedtime', pastWT); // update the embedded data value
jQuery("#msg2").show(); // display the message, the elapsed time is calculated above
});
});

These codes don't work. I have searched for the reason, is it because the embedded data's new set value can only be displayed in the next block? But I want to display this information in this block. How can I do it? Thanks!!

Hi cm201623,
I have made some changes in the above code, and now it shows the difference. Please refer to the code below:
HTML in question text:

Timer



JavaScript:
var starttime = 0;

Qualtrics.SurveyEngine.addOnload(function() {
starttime = new Date().getTime(); // start time is the page on load time
});

Qualtrics.SurveyEngine.addOnReady(function()
{
  /*Place your JavaScript here to run when the page is fully displayed*/
jQuery("#TimeButton").on("click", () => {
var pastWT = new Date().getTime()-starttime; // elapsed time is the button clicked time - start time
Qualtrics.SurveyEngine.setEmbeddedData('elapsedtime', pastWT); // update the embedded data value
jQuery("#msg2").text(pastWT); // display the message, the elapsed time is calculated above
});
});


Leave a Reply