【问题标题】:How do I keep incrementing the Elapsed time variable while its in the while loop instead of starting from 0 seconds everytime如何在 while 循环中不断增加 Elapsed time 变量,而不是每次都从 0 秒开始
【发布时间】:2021-04-24 22:44:32
【问题描述】:

我有一个 while 循环,我在其中查找数组中的两个属性,如果它们不存在,那么我调用 sleep 函数 10 秒并再次查找这些属性。我希望那里有一个经过的时间,以便用户可以看到我们一直在寻找这些属性多长时间。

var flag1 = "false";
var flag2 = "false";

while (flag1 == "false" || flag2 == "false")
   for { //for loop where it looks for some attributes in an array and if found, changes the respective flag to true
   }

   //if conditions so if the flags are still false then prints not found and tells that it will check again
   if (flag1 == "false") {
       print ("Attribute 1 not found. Check again in 10 seconds");
   }
   if (flag2 == "false") {
       print ("Attribute 2 not found. Check again in 10 seconds");
   }
   
   //Stopwatch 
   var startTime = new Date();
   sleep (10000);
   var endTime = new Date();
   var timeDiff = endTime - startTime;
   timeDiff /= 1000;
   var seconds = Math.round(timeDiff % 60);
   print("Elapsed Time is " + seconds + "seconds");
}
print("Both attributes found."};

预期输出:

Attribute 1 not found Check again in 10 seconds.
Attribute 2 not found. Check again in 10 seconds.
Elapsed time: 0 seconds.

//after 10 seconds.
Attribute 1 not found Check again in 10 seconds.
Attribute 2 not found. Check again in 10 seconds.
Elapsed time: 10 seconds.

//after another 10 seconds.
Attribute 1 not found Check again in 10 seconds.
Attribute 2 not found. Check again in 10 seconds.
Elapsed time: 20 seconds.

//after another 10 seconds and assuming that the attribute was found in array this time.
Both attributes found.

当前输出

Attribute 1 not found. Check again in 10 seconds.
Attribute 2 not found. Check again in 10 seconds.
Elapsed Time is 10seconds
Attribute 1 not found. Check again in 10 seconds.
Attribute 2 not found. Check again in 10 seconds.
Elapsed Time is 10seconds

打印时经过的时间总是显示 10 秒,我希望它不断增加。我该怎么做?

【问题讨论】:

  • startTime移到循环之外?
  • @HereticMonkey 谢谢这确实有帮助,但如果在第一次迭代本身中找到这两个属性,它仍然会打印 Elapsed Time is .... 我该如何应对呢?如果在第一次迭代中找到这两个属性,我不希望它 sleep(1000) 或打印 elapsed time
  • sleep (10000) 是做什么的?也许看到Sleep in JavaScript - delay between actions

标签: javascript datetime for-loop while-loop


【解决方案1】:

只需移动startTime 声明

var startTime = new Date();

在while循环之前:

var startTime = new Date();

while (flag1 == "false" || flag2 == "false")
   .... 


   if (flag1 && flag2) {
     // if your condition is matched, exit the loop
     break;
   }
   //Stopwatch 
   sleep (10000);
   var endTime = new Date();
   var timeDiff = endTime - startTime; 
   timeDiff /= 1000;
   var seconds = Math.round(timeDiff % 60);
   print("Elapsed Time is " + seconds + "seconds");
}
print("Both attributes found."};
   

【讨论】:

  • 感谢这确实有帮助,但如果在第一次迭代本身中找到这两个属性,它仍然会打印Elapsed Time is ...。我该如何应对?
  • 只需在sleep 之前检查您的条件,如果为真,请使用break 语句退出循环;检查更新的代码
  • 感谢您的帮助!另外,我看到如果Elapsed time 超过 60 秒,它会从 0 秒重新启动。我希望它继续从 60 秒递增。我该怎么做?
  • 尝试用var seconds = (endDate.getTime() - startDate.getTime()) / 1000;计算日期差异,也检查这个线程stackoverflow.com/questions/13894632/…
  • 感谢您也完成了这项工作!但现在它也打印小数点了.. 比如100.323 seconds10.321 seconds。我只想要整数。我该如何解决?
猜你喜欢
  • 1970-01-01
  • 2012-11-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-23
  • 2018-03-15
  • 2011-04-14
  • 2018-10-13
相关资源
最近更新 更多