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
2 Answers
Reset to default 5You'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 usingvar
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>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745098932a4611154.html
评论列表(0条)