Skip to content
Advertisement

Display data repeatedly

I’m working on a website where I’m showing data from my MySQL database. The data in the database is added every minute and I want to print that value live on my website. I’m using the following PHP code for a random number:

$randomNumber = rand(0, 1000);

How can i get this number to keep refreshing every minute without refreshing my browser?

Advertisement

Answer

You could use javascript setInterval() method.

The setInterval() method calls a function or evaluates an expression at specified intervals (in milliseconds).

Read more about it in the docs

See this fiddle for an example for setInterval().

The above fiddle uses a script as follows

setTimeout(function(){ alert("Hello"); }, 3000);

which displays an alert every 3 seconds.

Instead of showing an alert, you can include your logic to calculate the random number, in the above example code.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement