So, I am trying to make a random number generator with HTML and JavaScript. I want it to have a thing where it goes up by one until it reaches the random number so it has an "animation" of some sort, but I can’t make it work. Here is my code:
document.getElementById("numbergenerator").onclick = generateNumber;
function generateNumber() {
numbervalue = Math.floor(Math.random() * 1001);
for(var i = 0; i < numbervalue; i++) {
setTimeout(goUpThing(i), 10);
}
document.getElementById("numberdisplay").innerHTML = numbervalue;
}
function goUpThing(number) {
document.getElementById("numberdisplay").innerHTML = number;
}
<html>
<body>
<h1>
<em>random stuff</em>
</h1>
<hr>
<h2 id="numberdisplay">
there is no number yet
</h2>
<div>
<button id="numbergenerator" type="button">
generate a random number
</button>
</div>
</body>
</html>
Can someone please help me? By the way I am using JSFiddle to run my code.
2
Answers
You can use
setInterval()
andclearInterval()
.The
setInterval
allows a function to run at intervals set inms
( 1 in this example).It is assigned to a variable named
interval
for later reference.The inner function increments the
counter
on each interval until the condition is met – thecounter
is equal to randomnumbervalue
. Then the previous variableinterval
is passed toclearInterval
and it stops running the inner functionThere are several ways to achieve this.
async/await
in combination withsetTimeout
orrequestAnimationFrame
to wait for the next number to be displayed. (example 1 & 2)requestAnimationFrame()
-loop with an end condition. (example 5)setTimeout
orrequestAnimationFrame
to wait for the next number to be displayed. (example 6)And possibly some more.
All examples are displaying a random number from 1-1000.
The execution time of the example may vary.
requestAnimationFrame()
should be used if every number must be displayed at least once. It may be "slower" than an interval or timeout with 10ms as it depends basically on the refresh rate of the screen. On 60hz it will be similar to an interval with 17ms (1000 / 60 = 16.666~), while the setTimeout or setInterval callbacks may be called at the same frame multiple times.Example 3-6 will work also in older browsers without async/await support, you may also need to change the arrow functions to classic functions if required. Example 6 may also require a Promise polyfill if Promise isn’t supported.
Example 1
Generate a random number from 1 to 1000, inside the loop wait for the timeout to end before the script continues.
Example 2
Similar to #1, it just waits for the next frame.
Example 3
Start multiple timeouts at once, each delayed by
10 * numberToDisplay
ms.Example 4
Using setInterval with an end condition.
Example 5
Similar to #4, but this one uses requestAnimationFrame with an end condition and increases the passed value each time the function is called starting from 0 with the first number displayed is 1.
Example 6
This works similar to #1 and #2 and makes sure that the next number is only displayed, after the promise is fulfilled.