2014-07-18 121 views
1

我有这个while循环:如何让我的while循环每分钟启动一次?

while (now() > timeToday(checkTime).time && now() < timeToday(arrivalTime).time){ 

我想这个循环运行,只有当实时时钟的秒命中00基本上使循环每分钟运行一次。我这样做是因为我写的IDE只允许最多暂停15秒。不幸的是sleep()不在SmartThings IDE

我在想这样的事情

while (now() > timeToday(checkTime).time && 
     now() < timeToday(arrivalTime).time && 
     Date.parse('ss', now() = 00).time){ 

在所有的选项,但我与它摸索。

如果有更好的方法来做到这一点比有条件,请让我知道。

+0

任何原因'Thread.sleep'(或Groovy的默认静态'睡眠')将无法正常工作? –

+3

它是什么IDE? –

+0

SmartThings IDE – Brian

回答

2

SmartThings终于给出了一个答案,并表示使用他们的runIn(60, myHandler)逻辑。

感谢您的每一个输入。

1

你可以尝试sleep 60*1000来遍历下一个周期

while true 
{ 
    sleep 60*1000 
    //do your stuff here 
    //make sure to include break logic to exit out of the loop 
} 
+0

IDE不会允许'sleep()':( – Brian

+0

'java.lang.SecurityException:调用类script14057093772401260321749.sleep()不允许@ line 140' – Brian

2

对于方案之前,在一个循环中一分钟的睡眠,如“每分钟一次”,你可以看看TimerTask可以在很容易地使用常规的方式。

new Timer().schedule({ 
    // your logic here 
} as TimerTask, startTime, 1000) 

我建议阅读javadoc彻底了解调度合约(tick的正确性)。