[英]How to make a variable timer sequence which runs infinitive
I have n
parameters which have to be run in sequence for t
seconds each. 我有n
参数,每个参数必须依次运行t
秒。 All the data is stored in an array which is loaded dynamically through ajax and json and exists of: function parameters p
and time to sleep t
所有数据都存储在一个数组中,该数组通过ajax和json动态加载,并且存在:函数参数p
和睡眠时间t
function(p1)
, for 30 seconds; function(p1)
,持续30秒; when it completes function(p2)
for 15 seconds etc etc 当它完成function(p2)
15秒等时
Until the array is complete; 直到数组完成为止; then we have to start all over. 然后我们必须重新开始。 The number of parameters and its time being displayed are not determined on forehand. 参数的数量及其显示的时间不是预先确定的。
How can I implement this with javascript? 如何使用javascript来实现呢?
//edit: I tried to make one big function with function(p1) starting at t=0; // edit:我试图从t = 0开始用function(p1)制作一个大函数; function(p2) starting at t=t1; 从t = t1开始的函数(p2); function(p3) starting at t=t1+t3 从t = t1 + t3开始的函数(p3)
But it felt 'stupid' and overdone.... 但是感觉“愚蠢”和过头了。
I suspect you array looks like 我怀疑你数组看起来像
var myarray=[['p1',30],['p2',15] ...];
In this case you could 在这种情况下,您可以
function runme(i) {
if (i>=myarray.length) i=0;
var p=myarray[i][0];
var t=myarray[i][1];
myfunction(p);
i=i+1;
window.setTimeout('runme('+i+');',1000*t);
}
Edit 编辑
And ofcourse 而且当然
runme(0);
to start. 开始。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.