lundi 6 avril 2015

Display figure and then change it randomly every 5 seconds (javascript)

I'd like to display a random number between 1000 and 2000 every 5 seconds.


I manged to do it with javascriptMath random() and setInterval BUT I have a problem: on page load, the first number only appears after 5 seconds so the web visitors see nothing.


I'd like the first number to appear straight from the very first second and then wait for 5 second and get the second number then 5 seconds wait the third number, and so on...



<div id="my_number"></div>

setInterval(function() {
var number = 1000 + Math.floor(Math.random() * 1000);
$('#my_number').text(number);
},
5000); // every 5 seconds


How can I display right away a number and then trigger this 5-sec loop of random numbers?


Thanks





Aucun commentaire:

Enregistrer un commentaire