Javascript时间:如何每5、7和8秒执行一次函数? [英] Javascript Timing: How do I execute a function every 5, 7, and 8 seconds?
问题描述
假设我有一个函数log()
var log = function(i) {
console.log('Executing at time: ' + i);
}
我想每x,y和z秒间隔执行一次log()
吗?
And I want to execute log()
on an interval, every x, y, and z seconds?
让我们将x,y和z分别设置为5、7和8.
Let's arbitrarily set x, y, and z to 5, 7, and 8.
var x = 5;
var y = 7;
var z = 8;
我想每隔x,y和z秒调用一次log()
:
And every x, y, and z seconds I want to call log()
:
// Every 5 seconds:
log(x);
// Every 7 seconds:
log(y);
// Every 8 seconds:
log(z);
我可以尝试使用三个setTimeout()
来单独运行这些功能.但是,等一下,javascript不是异步的,因此执行一个setTimeout()
将会阻止执行另外两个...嗯...
I could use try using three setTimeout()
s to run these functions alone. But wait, javascript isn't asynchronous, so the execution of one setTimeout()
will prevent the execution the other two... Hmm...
我可以计算时间差,以使第一个setTimeout()
引起的延迟不会破坏第二个和第三个的时间:
I could calculate the difference in the times, so that the delay caused by the first setTimeout()
won't break the timing of the second and third:
// x is at 5 seconds, run 5000 milliseconds after the start
setTimeout(function() { log(x); }, 5000);
// y is at 7 seconds after start, or 2000 milliseconds after x is done
setTimeout(function() { log(y); }, 2000);
// z is at 7 seconds after start, or 1000 milliseconds after y is done
setTimeout(function() { log(z); }, 1000);
但是时间永远不会完全正确,因为我的log()
函数的执行将至少花费少量时间.另外,当我尝试在其中添加更多log()
命令时,或者当我尝试按设定的时间间隔重复整个过程时,这会变得很混乱.
But the timing would never be exactly right, as the execution of my log()
function will take at least a small amount of time. Also, this get's messy when I try and add more log()
commands in, or when I try and repeat the entire process at a set interval.
我可以使用循环,计算秒数,并在x,y和zth秒执行命令:
I could use a loop, count the seconds, and on the x, y, and zth second, execute my commands:
var i = 0;
while(i<10) {
// wait 1 second (pointless function, don't know a better way to wait a second)
setTimeout(function() { console.log(' ... '); }, 1000);
i++;
// execute x, y, or z depending on what time it is
switch(i) {
case 4:
log(x);
case 6:
log(y);
case 7:
log(z);
}
}
但是这种计数"方法(我在其中使用setTimeout()执行无用的命令)实际上使我感到效率低下.
But this method of 'counting', where I execute a useless command using setTimeout(), really just bothers me as inefficient.
是否有更好的方法来测量javascript中的时间,并让函数在将来的特定时间执行?对我来说,关键是它不只是每5秒运行一次log()
",因为我理解这一点.是在时间x,y和z时运行log()
"使我感到困惑.
Is there a better way to measure time in javascript, and have a function execute at certain times in the future? The key for me is that it's not just "run log()
every 5 seconds", because I understand that. It's "run log()
at times x, y and z" that confuses me.
推荐答案
您可以使用函数"setInterval".这是一个示例:
You can use the function "setInterval". Here is a example:
var interval1Id = setInterval(function(){
console.log("logging every 5 seconds");
},5000);
var interval2Id = setInterval(function(){
console.log("logging every 7 seconds");
},7000);
var interval3Id = setInterval(function(){
console.log("logging every 8 seconds");
},8000);
变量"intervalXId"被保存,以防万一您要停止任何间隔.
Variables "intervalXId" are saved just in case you want to stop any interval.
这篇关于Javascript时间:如何每5、7和8秒执行一次函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!