[英]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. 我正在使用AJAX从服务器获取不同的数据,但是某些数据不那么重要或更新不那么频繁。 So, I need to update in different intervals or set setTimeout.
因此,我需要以不同的间隔进行更新或设置setTimeout。 (please no JQuery)
(请不要使用JQuery)
I read here that setTimeout is alot better, because it doesn't cancel out other timed event like setInterval does. 我在这里读到setTimeout更好,因为它不会像setInterval一样取消其他定时事件。
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
}
You can just call setTimeout multiple times in your initialization code something like this: 您可以在初始化代码中多次调用setTimeout,如下所示:
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.
因此,在每个函数内,在所有处理结束时复制相同的调用,以间隔一定时间再次调用该函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.