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

27

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()

Sign up to request clarification or add additional context in comments.

2 Comments

how to make it between minutes. set 1000 to 60000 is gonna works.?
I thought this would trigger a stack overflow, but it works very well, thanks
12

You can do it like this:

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

myFunction()

1 Comment

thank you, brilliant! sorry, I can just mark only one question as answer, but your answer CodeiSir is also great!
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 Comment

This is quality.
2

Intervals, once set, are fixed.

Use setTimeout and call the function recursively instead.

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.