Animated Digital Dice

You can create an animated digital dice easily using the setInterval() method to be used as the timer.
Besides, we use the floor method and the random method to generate random integers from 1 to 6 using the following syntax:

Math.floor( 1 + Math.random() * 6 )

The full script is as follows

<div style="border:4px solid blue; width:8%; text-align:center;padding:0"><h1 id="dice" style="font-size:300%">1</h1></div>
<br>
<button onclick="AniDice()">Roll Dice</button>
<button onclick="stopDice()">Stop</button>

<script>

function AniDice()
{
MyVar=setInterval(rolldice,20)
}

function rolldice()
{
var ranNum = Math.floor( 1 + Math.random() * 6 );
document.getElementById("dice").innerHTML = ranNum;

}
function stopDice()
{clearInterval(MyVar);}
</script>

View Animated Digital Dice here.

View Animated Graphical Dice here.

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on Reddit