smotru smotru - 1 year ago 87
Javascript Question

How to use setInterval with random number of miliseconds each time?

The function

is executed every 1000 miliseconds but i want it to be executed every
miliseconds.. is there any solution for this ? Thank you!

var random = 1000;
setInterval(function() {random = randomizator(60000,200000);} ,1000);
setInterval(function() {showRandom(random);}, random);
function randomizator(a,b)
return Math.floor(Math.random()*b) + a;
function showRandom(random)

DEMO: jsFiddle

Answer Source

Felix said it: If you want to change the interval every time, use setTimeout instead. Simplified example as I'm having trouble following exactly what you want your original code to do:

function doTheRandom() {
    random = randomizator(60000,200000);
    // Up to 1 second
    setTimeout(doTheRandom, randomizator(1000, 2000)); // 1-2 seconds
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download