Javascript 如何在while循环中不断增加已用时间变量,而不是每次从0秒开始

Javascript 如何在while循环中不断增加已用时间变量,而不是每次从0秒开始,javascript,datetime,for-loop,while-loop,Javascript,Datetime,For Loop,While Loop,我有一个while循环,我在数组中查找两个属性,如果它们不在那里,那么我调用sleep函数10秒钟,然后再次查找这些属性。我希望那里有一个经过的时间,这样用户就可以看到我们寻找这些属性已经有多长时间了 var flag1 = "false"; var flag2 = "false"; while (flag1 == "false" || flag2 == "false") for { //for loop w

我有一个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
声明

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."};
   

startTime
移到循环外?@Hereticsmonkey谢谢,这确实有帮助,但如果在第一次迭代中发现两个属性,它仍将打印经过的时间是。。。。我该如何应对?如果在第一次迭代中发现了这两个属性,我不希望它
sleep(1000)
或打印
appeased time
。sleep(10000)
做什么?可能会看到。谢谢这确实有帮助,但是如果在第一次迭代中发现了这两个属性,它仍然会打印
经过的时间是…
。我该如何应对呢?只需在
睡眠之前检查你的状况,如果是真的,用
中断
语句退出循环;检查更新的代码谢谢你的帮助!另外,我看到如果
经过的时间超过60秒,它将从0秒重新启动。我希望它从60秒继续递增。我该怎么做呢?试着用
var seconds=(endDate.getTime()-startDate.getTime())/1000计算日期差,检查此线程也谢谢你也做了这项工作!但现在它也打印小数。。比如
100.323秒
10.321秒
。我只想要整数。我该如何解决这个问题?