如何判断一个setInterval是否是阻止应用程序退出的最后一件事

有没有办法知道有多less任务节点必须完成之前,过程会自动退出,因为没有别的可做的事情? 我想使用setInterval但只有当应用程序正忙。 我不想阻止该进程自动启动退出事件:

 // an app that does serveral things, then finishes // this should only be active whilst the app is busy: var interval = setInterval(function(){ // some stuff // how to determine if the interval is the last thing to complete? if(process.lastThingToDo == interval) clearInterval(interval); }, 2500); 

如果你想使用一个仍然像普通计时器一样工作的计时器,但是不会停止退出进程,你可以在其上使用.unref()

 // an app that does serveral things, then finishes // this should only be active whilst the app is busy: var interval = setInterval(function(){ // some stuff // how to determine if the interval is the last thing to complete? if(process.lastThingToDo == interval) clearInterval(interval); }, 2500); // add this .unref() call to make it so the process will not wait for this timer // before exiting, but the timer will otherwise work normally interval.unref(); 

用定时器查看.unref()的node.js文档 。