Javascript 如何判断setInterval是否是';正在阻止应用程序退出

Javascript 如何判断setInterval是否是';正在阻止应用程序退出,javascript,node.js,Javascript,Node.js,有没有办法知道节点必须完成多少任务,流程才会自动退出,因为没有其他事情要做?我想使用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

有没有办法知道节点必须完成多少任务,流程才会自动退出,因为没有其他事情要做?我想使用
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();
请参阅带有计时器的列表