最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

function - how to loop a random number in javascript - Stack Overflow

programmeradmin0浏览0评论

when I try to loop some functions it get stuck on the randomvariable() , how can the random number work on loop without I need to refresh the page everytime

function randomvariable() {
  randomvariable = Math.floor(Math.random() * 21);
  document.getElementById("demo").innerHTML = randomvariable;
}



function launchfunctions() {
  var count = 0;
  while (count < 10) {
    randomvariable();
    count++;

  }
}
launchfunctions();
<div id="demo" ></div>

when I try to loop some functions it get stuck on the randomvariable() , how can the random number work on loop without I need to refresh the page everytime

function randomvariable() {
  randomvariable = Math.floor(Math.random() * 21);
  document.getElementById("demo").innerHTML = randomvariable;
}



function launchfunctions() {
  var count = 0;
  while (count < 10) {
    randomvariable();
    count++;

  }
}
launchfunctions();
<div id="demo" ></div>

Share edited Feb 19, 2017 at 4:54 Rajesh 25k5 gold badges50 silver badges83 bronze badges asked Feb 19, 2017 at 4:44 zexurityzexurity 131 silver badge4 bronze badges 8
  • Most likely it's erroring as randomvariable is not declared. – Sid Commented Feb 19, 2017 at 4:54
  • 1 @Sid *is redeclared – Nick is tired Commented Feb 19, 2017 at 4:55
  • 1 @Sid Its declared and its getting overridden in function – Rajesh Commented Feb 19, 2017 at 4:55
  • 1 @zexurity Please add a habit of checking console for any errors. – Rajesh Commented Feb 19, 2017 at 4:55
  • what @Rajesh said... randomvariable is defined as a function in global scope, and within the function you're referencing the same function and changing the value to a random #. – gerald Commented Feb 19, 2017 at 4:59
 |  Show 3 more ments

2 Answers 2

Reset to default 5

You're redefining randomvariable, use var or call your variable something else.

function randomvariable() {
    var randomvariable = Math.floor(Math.random() * 21);
    document.getElementById("demo").innerHTML = randomvariable;
}

or

function randomvariable() {
    mynum = Math.floor(Math.random() * 21);
    document.getElementById("demo").innerHTML = mynum;
}

Although note that you should always use the var keyword when defining variables, it has been omitted in the second example to show how adjusting just your variable name could resolve the issue.

Note:

  • Declaring a variable without var will make it global in non-strict mode. This is the reason that using var inside the function will work.
  • Using the same variable names for a function and a variable make your code confusing and reduces readability. Use a better naming convention.

you are changing the property of the randomvariable function to variable with global scope so, when the flow entered into the randomvariable function the property get changed and reports an error msg. If you use var the variable is declared within the scope you are in (e.g. of the function).

I have modified the code to show the random numbers.

function randomvariable() {
  var randomvariable = Math.floor(Math.random() * 21);
  document.getElementById("demo").innerHTML += randomvariable + "-";
}

function launchfunctions() {
  var count = 0;
  while (count < 10) {
    randomvariable();
    count++;
  }
}
<html>

<body onload="launchfunctions()">
  <div id="demo"></div>
</body>

</html>

发布评论

评论列表(0)

  1. 暂无评论