8

I have this setinterval with function alert:

setInterval(function(){
    alert('oo');
}, 5000);

But I would like to change my (5000) interval each time interval runs alert() - I'd like have it randomly selected between 5 - 10 seconds. How can I do it?

2

5 Answers 5

25

You should use setTimeout to set interval after which the function should be executed.

function myFunction() {
  var min = 5,
    max = 10;
  var rand = Math.floor(Math.random() * (max - min + 1) + min); //Generate Random number between 5 - 10
  console.log('Wait for ' + rand + ' seconds');
  setTimeout(myFunction, rand * 1000);
}

myFunction()

1
  • how to make it between minutes. set 1000 to 60000 is gonna works.? Jan 7 at 17:10
12

You can do it like this:

function myFunction() {
  alert('oo');
  setTimeout(myFunction, Math.random() * 5000)
}

myFunction()

1
  • thank you, brilliant! sorry, I can just mark only one question as answer, but your answer CodeiSir is also great!
    – peter
    Jan 7, 2016 at 14:15
7

You can do this with two methods, first setTimeout and second requestAnimationFrame.

Here are the full examples of both.

Random interval with setTimeout

function randomInterval(callback, min, max) {
    let timeout;

    const randomNum = (max, min = 0) => Math.random() * (max - min) + min;

    const stop = () => clearTimeout(timeout)

    const tick = () => {
        let time = randomNum(min, max);
        stop();

        timeout = setTimeout(() => {
            tick();
            callback && typeof callback === "function" && callback(stop);
        }, time)
    }

    tick();
}

Random interval using requestAnimationFrame

function randomInterval(callback, min, max) {
    const randomNum = (max, min = 0) => Math.random() * (max - min) + min;

    let targetTime = randomNum(min, max);
    let lastInvoke = performance.now();

    const stop = () => targetTime = null

    const tick = () => {
        if (!targetTime) return;

        if (performance.now() - lastInvoke > targetTime) {
            lastInvoke = performance.now();
            targetTime = randomNum(min, max);
            callback && typeof callback === "function" && callback(stop);
        }

        requestAnimationFrame(tick)
    }

    tick();
}

and here is an example of how to call it

randomInterval((stop) => {
    // do what you want...

    if (stopCondition) {
        stop();
    }
}, 1000, 3000)
1
  • This is quality.
    – StayCool
    Aug 14, 2022 at 20:58
1

Intervals, once set, are fixed.

Use setTimeout and call the function recursively instead.

function myAlert() {
    setTimeout(myAlert, generate_random_time());
    alert('oo');
}
0

The only potential issue with most of the current answers here is that the initial function call does not have the random delay, only the subsequent calls.

A modification is to place the function outside the loop then call it inside the setTimeout:

function doSomething() {
    console.log("Hello");
}

(function loop() {
    var rand = Math.round(Math.random() * 10);
    setTimeout(function() {
            doSomething();
            console.log("Delayed " + rand + " secs.");
            loop();  
    }, rand*1000);
}());

Learning ES6 converting to arrow functions and template literals the code looks like:

function doSomething() {
    console.log("Hello");
}

(function loop() {
    let rand = Math.round(Math.random() * 10);
    setTimeout( () => {
            doSomething();
            console.log(`Delayed ${rand} secs`);
            loop();  
    }, rand*1000);
}());

Adapted from a very similar thread:https://stackoverflow.com/questions/6962658/randomize-setinterval-how-to-rewrite-same-random-after-random-interval

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.