Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
94
rated 0 times [  97] [ 3]  / answers: 1 / hits: 28413  / 9 Years ago, thu, january 7, 2016, 12:00:00

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?


More From » javascript

 Answers
253

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




[#63808] Tuesday, January 5, 2016, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
saulthomasb

Total Points: 326
Total Questions: 98
Total Answers: 93

Location: Jordan
Member since Sun, Dec 26, 2021
2 Years ago
;