简体   繁体   English

为什么这个简单的javascript函数无法实现预期的功能?

[英]Why won't this simple javascript function do what it is supposed to?

I am trying to write a function that will make a string of random numbers appear one after the other inside an html 我正在尝试编写一个函数,该函数将使一串随机数在html内一个接一个地出现

element. 元件。 See the function below: 参见以下功能:

document.getElementById("button-1").onclick = function () {
  var numbersArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
  var newNumbersArray = [];

  function createNewArray (array) {
    for (i=0;i<50;i++) {
      array.push(numbersArray[Math.floor(Math.random()*11)]);
    }
    return array;
  }

  for (j = 0; j < newNumbersArray.length; j++) {
    document.getElementById("number-display").innerHTML = newNumbersArray[j];
  }
}

The first part of the function takes an array of numbers 0-10 and creates a new array 50 elements long made up of random numbers from the variable numbersArray. 函数的第一部分采用数字0-10的数组,并创建一个新的包含50个元素的新数组,这些元素由变量numbersArray中的随机数组成。 Then the function is supposed to display each number one after the other on the html page (note that I am aware this would happen so fast that you would only see the last number. I plan to later add a timer, right now I am just trying to get the basic functionality). 然后该函数应该在html页面上一个接一个地显示每个数字(请注意,我知道这会发生得如此之快,以至于您只会看到最后一个数字。我打算稍后添加一个计时器,现在我只是尝试获取基本功能)。 However, when I click the button the html page, absolutely nothing happens. 但是,当我单击html页面按钮时,绝对没有任何反应。 What am I missing? 我想念什么?

Many thanks for your help. 非常感谢您的帮助。 I am a newbie (probably quite obvious). 我是新手(可能很明显)。

The problem is that you never called createNewArray 问题是您从未调用过createNewArray

 document.getElementById("button-1").onclick = function () { var numbersArray = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]; var newNumbersArray; function createNewArray (array) { for (i=0;i<50;i++) { array.push(numbersArray[Math.floor(Math.random()*11)]); } return array; } // this is required newNumbersArray = createNewArray([]); for (j = 0; j < newNumbersArray.length; j++) { document.getElementById("number-display").innerHTML = newNumbersArray[j]; } } 
 <div id='number-display'></div> <button id="button-1">Action</button> 

You're not calling createNewArray() , maybe you're missing: 您没有调用createNewArray() ,也许您丢失了:

newNumbersArray = createNewArray(numbersArray)

Also, this looks weird because you're overwritting the value multiple times: 另外,这看起来很奇怪,因为您多次覆盖了该值:

for (j = 0; j < newNumbersArray.length; j++) {
  document.getElementById("number-display").innerHTML = newNumbersArray[j];
}

You could do: 您可以这样做:

document.getElementById("number-display").innerHTML = '';
for (j = 0; j < newNumbersArray.length; j++) {
  document.getElementById("number-display").innerHTML += newNumbersArray[j] + ",";
}

But there is an easier way: 但是有一种更简单的方法:

document.getElementById("number-display").innerHTML = newNumbersArray[j].join(',');

The issue that I found in your code is that you are defining a function called createNewArray but you never use it. 我在您的代码中发现的问题是,您正在定义一个名为createNewArray函数,但从未使用过。

The solution should be: 解决方案应该是:

createNewArray(newNumbersArray)

However, I'd recommend you to do the following: 但是,建议您执行以下操作:

 document.getElementById("button-1").onclick = function () { var newNumbersArray; function createNewArray () { // instead of passing a parameter which will be modified, // you can create a variable inside your function and return it var array = []; for (i=0;i<50;i++) { // array.push(numbersArray[Math.floor(Math.random()*11)]) // the line above is practically the same as the line bellow array.push(Math.floor(Math.random()*11)); } return array; } // so you can use it in this way newNumbersArray = createNewArray(); for (j = 0; j < newNumbersArray.length; j++) { document.getElementById("number-display").innerHTML = newNumbersArray[j]; } } 
 <div id="number-display"></div> <button id="button-1">Action</button> 

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

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