简体   繁体   English

如何设置多个(可能是两个以上)函数在不同的时间使用setTimeout或setInterval执行?

[英]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.

相关问题 如何让 setTimeout 调用 setInterval 使其在一段时间后执行? - How to make a setTimeout to call a setInterval to make it execute after some time? 如何在setInterval和setTimeout函数中更改“this”的范围 - How do I change scope of “this” in setInterval and setTimeout functions setTimeout / setInterval有多个延迟来执行代码 - setTimeout/setInterval with multiple delays to execute code 在下面的时钟代码中,setTimeout如何像setInterval一样运行,即使setTimeout仅执行一次? - how setTimeout act like setInterval in the clock code below even setTimeout execute one time only? 如何使用setInterval或setTimeOut同步执行? - How to execute synchronously using setInterval or setTimeOut? 如何用Javascript一次打开多个窗口? - How do you open more than one window at a time in Javascript? 你如何处理setTimeout()的多个实例? - How do you handle multiple instances of setTimeout()? setTimeout() 和 setInterval() 函数是异步的吗? - Are setTimeout() and setInterval() functions async? 如何在不同的时刻触发不同的 setInterval 函数? - How to fire different setInterval functions at different moments of time? 如何正确使用 setInterval 和 clearInterval 在两个不同的函数之间切换? - How do I correctly use setInterval and clearInterval to switch between two different functions?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM