在for循环中以1分钟为间隔增加每个元素 [英] Increment each element by 1 minute interval in for loop
本文介绍了在for循环中以1分钟为间隔增加每个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家早上好,
我有一个关于1分钟间隔后for循环增量的查询,即如果我在一个数组中有5个元素并且我想以1分钟间隔执行所有它们.
请帮我实现它.
Good Morning All,
I have one query regarding increment of for-loop after 1 minute interval, i.e like if I have 5 elements in an Array and I want to execute all them with 1 minute interval.
Please help me how can i achieve it.
推荐答案
您不需要真正的for循环.
您需要一个每分钟到期的计时器.
当它过期时,您将递增用作数组索引的内部变量.
例如,在客户端javascript中,您有2种window对象的方法:
setTimeout:安排函数在指定的毫秒数后运行
setInterval:相同,但重复调用该函数:var h = setInterval(callback,60000)
clearInterval:可用于清除计时器:clearInterval(h)
You don''t need a real for loop.
You need a timer that expires every minute.
When it expires you increment an internal variable that you use as index into the array.
For example in client side javascript you have 2 methods of the window object:
setTimeout : schedules a function to run after a specified number of miliseconds
setInterval : is the same but invokes the function repeatedly :var h = setInterval(callback,60000)
clearInterval : can be used to clear the timer : clearInterval(h)
Class CustomTimer
{
private DateTime startTime;
private DateTime stopTime;
private bool running = false;
public void Start()
{
this.startTime = DateTime.Now;
this.running = true;
}
public void Stop()
{
this.stopTime = DateTime.Now;
this.running = false;
}
//this will return time elapsed in seconds
public double GetElapsedTimeSecs()
{
TimeSpan interval;
if (running)
interval = DateTime.Now - startTime;
else
interval = stopTime - startTime;
return interval.TotalSeconds;
}
}
现在在您的foreach循环中执行以下操作:
Now within your foreach loop do the following:
CustomTimer ct = new CustomTimer();
ct.Start();
// put your code here
ct.Stop();
//timeinsecond variable will be set to time seconds for your execution.
double timeinseconds=ct.GetElapsedTime();
您可以为定义的间隔使用长dummy for loop
或使用JavaScripttimer
.
有关计时器,请参阅: JavaScript计时器 [
You can use a longdummy for loop
for defined interval OR use a JavaScripttimer
.
For timer, refer: Javascript timers[^]
这篇关于在for循环中以1分钟为间隔增加每个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文