Creating a random number generator in HTML

I am trying to create a web page that looks like a health monitor, and I need to create a random number generator that will act like a heart monitor. Here is what I have:

<?php

function functionName()
{
    return rand(5, 15);
}

?>

<html>
<body>

<?php 

    $i = 0;

while ($i <= 10) 
{
    echo functionName();
    echo "</br>";
    $i++;
}

?>


</body>
</html>

The problem is that the number is printed one by one, and I need them to just display in the same place, but be different. In other words, if I have a section that says "Heart Best Per Seconds:", I need a new number that appears there every few seconds instead of another.

Does anyone know how to do this? I have seen similar things, so I'm sure it is possible.

+4
source share
3 answers

setInterval(function() {
  var i = Math.floor(Math.random() * (15 - 5 + 1)) + 5;
  document.getElementById("random").innerHTML = i;
}, 1000);
 <span id="random"></span>
Hide result
, ? Math.random(); setInterval()
+3

, , , , JavaScript JQuery PHP. randomnumber.php

<?php
die(rand(5,15));
?>

index.php

<html>
    <head>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.2/jquery.min.js"></script> <!-- load JQuery from Google CDN -->
    </head>
    <body>
        <h2 id="randomNumber"></h2>
    </body>
    <script>
    function getRandom() {
        setInterval(function() {
            $("#randomNumber").load("randomNumber.php");
        }, 3000) // delay in milliseconds
    }
    getRandom();
    </script>
</html>
0

You can generate random numbers using Math.random().

Circle the number down to its nearest integer with Math.floor().

And use setInterval()to run the function at a certain interval.

setInterval(function() {
  var i = Math.floor(Math.random() * (15 - 5 + 1)) + 5;
  document.getElementById("hbeat").innerHTML = 'Heart Beat Per Seconds : ' + i;
}, 1000);
<span id="hbeat"></span>
Run codeHide result
0
source

All Articles