等待循环,直到计数器减少

时间:2018-08-27 19:06:02

标签: node.js wait slot

我想循环等待直到计数器减少。这可能吗?此刻,我想我需要等待x秒并检查计数器。然后再次检查。但是有更好的解决方案吗?

所以看看我的意思:

counter=0;
for(x=1;x<100;x++){
 if(counter<10){
  counter++;
  dothings
 }else{
  wait until counter<10
  do things
 }
} 
function dothings(){
 calculate;
 counter--;
}

所以仅是为了说明我的意思。

1 个答案:

答案 0 :(得分:2)

将代码样式转换为事件驱动,使用一种方法减少计数器,如果满足条件,则调用另一种方法来“执行操作”。

相关问题