0

我正在创建一个应该自动停止的程序<b>。所以请告诉我如何,这是我正在考虑的一点:

var time = date.getTime();
var seconds = 0;
if (time + seconds == date.getTime())
{
// Stop everything i do by myself :)
}
4

2 回答 2

4

像这样的东西:

var keepGoing = true;


setTimeout(function(){

    keepGoing = false;

}, 1000 * 60 * 60); //one hour


while(keepGoing){

    //stuff to do

}
于 2017-02-10T15:49:06.300 回答
0

我们可以使用setInterval和的组合setTimeout来实现这一点。

function doStuff(){
    //do whatever you need to do
}

var is_running = true;

setTimeout(function(){
    is_running = false; //Stop the program
}, 1000 * 60 * 60); //Do this after 1 hour

while(is_running){
    doStuff(); //whatever your function does
}

于 2021-07-19T23:58:52.937 回答