0

For some reason, I cannot get my global variable counter to increase, even when it increases within the function I have the count++ occurring in. My outputted results are different between the text outputted within the function and the text outside of it. Any idea what I am doing wrong here? Shouldn't the count increase on each iteration of the survey.oncomplete function results?

Survey
    .StylesManager
    .applyTheme("modern");

var kn2 = "LwrHXqFRN_pszCopTKHF_Q"
var kn3 = "exroCUoYl4wVzs7pKU_49w"

var count = 0

var keyname = ("kn" + count)

var mapilink = "https://images.mapillary.com/" + (keyname) + "/thumb-1024.jpg";

var json = {
 pages: [
  {
   name: "page1",
   elements: [
    {
     type: "image",
     name: "image",
     imageLink: (mapilink),
     imageHeight: 580,
     imageWidth: 640
    },
    {
     type: "html",
     name: (keyname),
     visible: false,
     html: (keyname)
    },
    {
     type: "rating",
     name: "Walkability",
     title: "How walkable does this look to you"
    },
    {
     type: "rating",
     name: "Saftey",
     title: "How safe does this look to you"
    },
    {
     type: "rating",
     name: "Comfortability",
     title: "How comfortable does this look to you"
    }
   ]
  }
 ]
}

window.survey = new Survey.Model(json);

var username = document.getElementById("user").value;

survey
    .onComplete
    .add(function (result) {
        count ++;
        var PID = document.getElementById("user").value;
        var results = PID + "_" + (keyname) + ":\n" + JSON.stringify(result.data, null, 3) + (count) ;
        document
            .querySelector('#surveyResult')
            .textContent = results;
    survey.clear();
    survey.render();
    });

$("#surveyElement").Survey({model: survey});
isherwood
  • 58,414
  • 16
  • 114
  • 157
  • Does this answer your question? https://stackoverflow.com/questions/23667086/why-is-my-variable-unaltered-after-i-modify-it-inside-of-a-function-asynchron – isherwood Dec 01 '20 at 17:56
  • Maybe post a code snippet. Is it your intention that keyname always equal kn0, because right now incrementing count will not affect the keyname variable. – user2258152 Dec 01 '20 at 18:39
  • hi, that is the problem - I would like keyname to increase with the count i.e. kn 0, kn1, kn2, kn3 etc etc – StupidQuestionGuy Dec 01 '20 at 19:07
  • Hi, I am still wondering about that, I've tried several things. @user2258152 – StupidQuestionGuy Dec 04 '20 at 21:01

1 Answers1

0

Got an answer from a seperate stackexchange post - basically, I needed to wrap everything in more functions.

function outputting function text rather than expected output