toastext
toastext

Reputation: 335

How do you set up multiple(maybe more than two) functions to execute at different time with setTimeout or setInterval?

I'm using AJAX to get different data from the server, but some data aren't as important or isn't updated as frequently. So, I need to update in different intervals or set setTimeout. (please no JQuery)

I read here that setTimeout is alot better, because it doesn't cancel out other timed event like setInterval does.

function ajax_one(){
//do something
}

function ajax_two(){
//do something
}

function nest(){
//call function
function ajax_one();
//run function one every min

function ajax_two(){}
//run function two every 6 Minutes
}

Upvotes: 0

Views: 30

Answers (1)

bitfiddler
bitfiddler

Reputation: 2115

You can just call setTimeout multiple times in your initialization code something like this:

setTimeout( ajax_one, 6000 ); // call in a minute
setTimeout( ajax_two, 36000 ); //call in six minutes

The timer is automatically cancelled when the event fires. So, within each function, copy the same call at the end of all processing to call the function again at the interval.

Upvotes: 2

Related Questions