我想知道是否有可能用计时器打破一个while循环? 在互联网上查找但无法找到解决方案。
MATCH (n:Node { id: 'root' }),
paths = (n)<-[:CHILD_OF*]-(children:Node { expanded: true })
RETURN collect(nodes(paths))
谢谢。
答案 0 :(得分:1)
您应该使用setTimeout
。
var timer = 3;
setTimeout(excuteMethod, 1000);
function excuteMethod() {
alert(timer + ' call');
timer--;
if (timer >= 0) setTimeout(excuteMethod, 1000);
}
答案 1 :(得分:0)
我不确定这是否是正确的方法,但它确实有效,
(function() {
var delay = 30;
var date = new Date();
var timer = date.setTime(date.getTime() + delay);
var count = 0;
function validate() {
var now = new Date();
if (+now > timer)
return false;
else
return true;
}
while (true) {
count++;
console.log(count);
if (!validate()) {
console.log("Time expired");
break;
}
// Fail safe.
if (count > 50000) {
console.log("Count breached")
break;
}
}
})()
答案 2 :(得分:0)
您可以更改计时器功能中的控制值并打破循环。
var control = true;
while(control)
{
...
}
setTimeout(function(){
control = false;
}, delay); //delay is miliseconds
或基于计数器
var control = true,
counter = 10;
while(control){
...
}
// you can handle as count down
// count down counter every 1000 miliseconds
// after 10(counter start value) seconds
// change control value to false to break while loop
// and clear interval
var counterInterval = setInterval(function(){
counter--;
if(counter == 0)
{
control = false;
clearInterval(counterInterval);
}
},1000);