简体   繁体   English

将函数内部生成的数组推送到全局变量

[英]Pushing array generated inside a function to a global variable

Can someone help me understand why I can't push an array to the generatedNumbers global variable with this function: 有人可以帮助我理解为什么我不能使用以下函数将数组推入generateNumbers全局变量:

var totalFrames = document.getElementById("totalFrames").value;
var framesToSkip = document.getElementById("framesToSkip").value;
var generatedNumbers = [];

function clickGenerate() {
  for (var i = 1; i <= totalFrames; i++) {
    if ((i % framesToSkip) === 0) {
      // do nothing
    } else {
      generatedNumbers.push(i);
    }
  }
  document.getElementById("numberList").innerHTML = generatedNumbers;
}

You can see it accompanied by the html here: 您可以在此处看到伴随HTML的内容:

https://jsfiddle.net/bdorrance/wszzvpu1/ https://jsfiddle.net/bdorrance/wszzvpu1/

If I put the variables inside the function, it works fine, but I need the variables to be global so they can be accessed by other functions. 如果将变量放入函数中,则可以正常工作,但是我需要将变量设置为全局变量,以便其他函数可以访问它们。 You can see that code here: 您可以在这里看到该代码:

https://jsfiddle.net/bdorrance/t246rnxe/ https://jsfiddle.net/bdorrance/t246rnxe/

You're defining your totalFrames and framesToSkip variables before you enter anything into those input boxes or clicking the button to generate. 在将任何内容输入到这些输入框中或单击按钮进行生成之前,您需要定义totalFramesframesToSkip变量。 There is nothing in the boxes, so there is nothing to grab, and nothing to push to the array. 盒子里没有东西,所以没有东西要抓住,也没有东西要推到阵列上。

In your jsfiddle with the variables included, the variables are not defined until the button is clicked, at which point you have numbers already entered in the box. 在包含变量的jsfiddle中,直到单击按钮后才定义变量,此时您已经在框中输入了数字。

You can make the variables global by defining them initially outside of the function, eg var totalFrames, framesToSkip; 您可以通过首先在函数外部定义变量来使变量成为全局变量,例如var totalFrames, framesToSkip; , then assigning to them the values of the input boxes within your onClick function. ,然后为它们分配onClick函数中输入框的值。

Revised JS: 修订版JS:

var totalFrames, framesToSkip;

function clickGenerate() {
    totalFrames = document.getElementById("totalFrames").value;
    framesToSkip = document.getElementById("framesToSkip").value;
    var generatedNumbers = [];

    for (var i = 1; i <= totalFrames; i++) {
        if ((i % framesToSkip) === 0) {
            // do nothing
        } else {
            generatedNumbers.push(i);
        }
    }
    document.getElementById("numberList").innerHTML = generatedNumbers;
}

function showFullResult() {
    document.getElementById("numberList").innerHTML = "full test";
}

function showShortResult() {
    document.getElementById("numberList").innerHTML = "short test";
}

Here is an updated example in jsfiddle 这是jsfiddle中的更新示例

your problem is not that global array. 您的问题不是那个全局数组。 Your problem is that you dont gather the actual valus of "totalFrames" and "totalFramesSkip". 您的问题是您没有收集“ totalFrames”和“ totalFramesSkip”的实际值。 As the function is invoked. 当函数被调用时。 The values of the variables totalFrames and framesToSkip are 0 变量totalFramesframesToSkip0

You have to gather the actual values on function invocation. 您必须在函数调用时收集实际值。 Add these lines IN your function: 在您的函数中添加以下行:

framesToSkip = document.getElementById("framesToSkip").value;
totalFrames = document.getElementById("totalFrames").value;

Then it should work 那应该工作

As others have said: move the variables for totalFrames and framesToSkip inside your function: 正如其他人所说:将totalFrames和framesToSkip的变量移入函数中:

var totalFrames;
var framesToSkip;
function clickGenerate() {
    totalFrames = document.getElementById("totalFrames").value;
    framesToSkip = document.getElementById("framesToSkip").value;
    console.log(totalFrames)
    for (var i = 1; i <= totalFrames; i++) {
        if ((i % framesToSkip) === 0) {
            // do nothing
        } else {
            generatedNumbers.push(i);
        }
    }
    document.getElementById("numberList").innerHTML = generatedNumbers;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM