【问题标题】:Global Count Variable not increasing全局计数变量不增加
【发布时间】:2020-12-01 17:53:52
【问题描述】:

由于某种原因,我无法让我的全局变量 counter 增加,即使它在函数内增加,我也有 count++ 发生。我的输出结果在函数内输出的文本和函数外的文本之间是不同的。知道我在这里做错了什么吗?在survey.oncomplete函数结果的每次迭代中计数不应该增加吗?

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});

【问题讨论】:

  • 这能回答你的问题吗? stackoverflow.com/questions/23667086/…
  • 也许贴一个代码 sn-p。您是否打算让 keyname 始终等于 kn0,因为现在递增计数不会影响 keyname 变量。
  • 嗨,这就是问题所在 - 我希望键名随着计数的增加而增加,即 kn 0、kn1、kn2、kn3 等
  • 嗨,我仍然想知道,我已经尝试了几件事。 @user2258152

标签: javascript surveyjs


【解决方案1】:

从一个单独的 stackexchange 帖子中得到答案 - 基本上,我需要将所有内容包装在更多函数中。

function outputting function text rather than expected output

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-14
    • 1970-01-01
    • 2021-07-16
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    相关资源
    最近更新 更多