-2

So I am a total JS novice, but I had someone write some code to be used within Qualtics survey. That code is now broken and I am trying to fix it up, there is an error with this line:

var timingObj=${e://Field/TimingObj};

I was hoping someone could help explain this line to me so I may be able to fix it up. (I have to full code, but that seemed a bit long to have someone go through.) I understand the first part is setting the variable named timingobj equal to something.

What that is is confusing to me, especially with the "//" which I understand to denote a note and not code. This code had worked in the past but now gives me an error. Any help understanding what I am working with would be amazing.

Thanks!

Update full code as provided to me:

//configurations
var bindInterval=10;

//initiate
var timingObj=${e://Field/TimingObj};



timingObj.version=3;

var startTiming=function(tag){
var currentTimeObj=timingObj[tag];
if (!currentTimeObj) {
    currentTimeObj={};
    currentTimeObj.startTimes=[];
    currentTimeObj.elapseTimes=[];
    currentTimeObj.totalElapsed=0;
    timingObj[tag]=currentTimeObj;
} 
var time=(new Date()).getTime();
currentTimeObj.startTimes.push(time);
currentTimeObj.startTime=time;
timingObj.activateTag=tag;
};

var closePopupCallback=function(){
//stop timing
var time=(new Date()).getTime();
var currentTag = timingObj.activateTag;
var currentTimeObj = timingObj[currentTag];
var elapsed=time - currentTimeObj.startTime;
currentTimeObj.elapseTimes.push(elapsed);
var totalElapsed=currentTimeObj.totalElapsed + elapsed;
currentTimeObj.totalElapsed=totalElapsed;
Qualtrics.SurveyEngine.setEmbeddedData(currentTag+'_Time',totalElapsed);
    Qualtrics.SurveyEngine.setEmbeddedData(currentTag+'_Count',currentTimeObj.startTimes.length);
var timingObjSerialized=Object.toJSON(timingObj);
Qualtrics.SurveyEngine.setEmbeddedData('TimingObj',timingObjSerialized);

};


var bindCloseEvent=function() {
//window.document.observe('dom:loaded',func) and document.observe('dom:loaded',func) did not work
var closeButton=$('bottomNavClose');
if (closeButton) {
    closeButton.observe('click', closePopupCallback);
} else{
    setTimeout(bindCloseEvent,bindInterval);
}
};
// bad smell
var bindLightBoxCloseEvent=function(){
var lightBox=$('lightbox');
if (lightBox) {
    lightBox.observe('click', closePopupCallback);
} else{
    setTimeout(bindLightBoxCloseEvent,bindInterval);
}
};

bindLightBoxCloseEvent();
bindCloseEvent();

Qualtrics.SurveyEngine.addOnload(function(){

});
I Smith
  • 19
  • 2
  • What is the error? – Pat Apr 03 '17 at 18:13
  • That looks like a template literal: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals – Hanlet Escaño Apr 03 '17 at 18:14
  • 5
    That must be some kind of templating because its not valid JavaScript as it stands ... – Alex K. Apr 03 '17 at 18:14
  • 2
    Looks like a template engine. Are you sure the JavaScript is not parsed by something else before being sent to the browser (i.e. Freemarker) – BCartolo Apr 03 '17 at 18:14
  • 1
    I added a Qualtrics tag assuming your question has a typo. – Alex K. Apr 03 '17 at 18:16
  • The error comes back: Unexpected token { – I Smith Apr 03 '17 at 18:22
  • There are no other back ticks but I am unsure how the template literal operates. – I Smith Apr 03 '17 at 18:24
  • I am not sure the JS doesn't get parsed by anything else, I add it under a section of the survey marked JS. Again I am no expert with how JS operates within Qualtrics. – I Smith Apr 03 '17 at 18:26
  • @HanletEscaño: That doesn't look like template literals. I Smith: The users talking about a template engine are not referring to the template literals that Hanlet referenced. –  Apr 03 '17 at 18:28
  • Possibly more context could help, the item leads into setting up a timer for a pop-up. For example a user clicks and it opens a pop-up this was intended to all an embedded variable to track the time spent with that pop-up open, It is combined with a click count to count the number of times a pop-up is opened within a single survey. – I Smith Apr 03 '17 at 18:30
  • Can you post tge error which you are receiving – EasyE Apr 03 '17 at 18:33
  • The error is: Unexpected token { – I Smith Apr 03 '17 at 18:33

1 Answers1

1

This should be an easy fix:

try:

var timingObj = "${e://Field/TimingObj}";

When using Qualtrics piped text in JavaScript, you must enclose the piped text in quotes.

If the piped text is a number that you plan on using and manipulating, as I am assuming, you should use parseInt() to ensure you don't have issues:

var timingObj = parseInt("${e://Field/TimingObj}");

Since the full code makes it clear that the item you are passing in is meant to be an object, I am assuming it is being passed in valid JSON, you should parse it as such:

var timingObj = JSON.parse("${e://Field/TimingObj}");
Anthony Rivas
  • 952
  • 13
  • 21
  • I attempted the parseInt solution, and while that does fix the error, the code is not working correctly. It is not tracking the time and count as it should be. But thank you for the input! – I Smith Apr 03 '17 at 19:07
  • If you update the post with a fuller version of your code, I would be happy to take a look for you. – Anthony Rivas Apr 03 '17 at 21:07
  • I have updated what I was given as the coding. It works with some embedded data and popups that I have placed into Qualtrics. I appreciate the assistance. – I Smith Apr 03 '17 at 21:14
  • I updated my answer with what I imagine should have been your use case from the start. rather than a number, you were passing an object. I can confirm this if you wouldn't mind sending me an example of ${e://Field/TimingObj} – Anthony Rivas Apr 03 '17 at 21:29
  • The timingObj are embedded variables items made up for the case, i.e. F_PF_Time, F_PF_Count, F_ES_Time F_ES_Count These activate in a popup within the survey, so when a user clicks on that popup these get recorded. – I Smith Apr 03 '17 at 22:58
  • Right, is there any chance you could add ${e://Field/TimingObj} to the page you are on and send me what it outputs? I imagine that it is stored in json, but can't confirm without seeing that output. – Anthony Rivas Apr 03 '17 at 23:09
  • I'm sorry I don't understand what you mean. Add it where? It is in my java code but the only output I view is in the form of a excel file with the end variables. Again very sorry super java novice. – I Smith Apr 03 '17 at 23:15
  • The output from the excel file could work just as well! Just one respondents final TimingObj field would help me to help you. – Anthony Rivas Apr 03 '17 at 23:20
  • After adding in the JSON.parse the TimingObj ended with this: {"version":3,"OF_G":{"startTimes":[1491262008381],"elapseTimes":[1816],"totalElapsed":1816,"startTime":1491262008381},"activateTag":"OF_G"} – I Smith Apr 03 '17 at 23:30
  • The initial goal, and when the code was working properly, left the embedded variable with a time value and a count value i.e. OF_G_Time xxx and OF_G_Count y. – I Smith Apr 04 '17 at 01:12
  • Alright, I think I'm understanding this, can you click on your question text, then on html, then copy that and add it as html here? I need to make sure I am understanding when these functions are being fired. – Anthony Rivas Apr 04 '17 at 21:37
  • Thanks for your help! (sorry I was traveling so the slow response) The html:   – I Smith Apr 08 '17 at 02:53