Javascript setInterval从未在函数中触发,不知道原因(角度)
我有一个函数,出于某种原因,它直接从打印“jobstart调用后提交组件行”到“输入while循环”:Javascript setInterval从未在函数中触发,不知道原因(角度),javascript,setinterval,Javascript,Setinterval,我有一个函数,出于某种原因,它直接从打印“jobstart调用后提交组件行”到“输入while循环”: returned() { this.numReturned++; if (this.numReturned !== this.numSubmitting) { return; } console.log('submit component line before jobstart call'); this.submitService.st
returned() {
this.numReturned++;
if (this.numReturned !== this.numSubmitting) {
return;
}
console.log('submit component line before jobstart call');
this.submitService.startJobToAuditTable().subscribe();
console.log('submit component line after jobstart call');
let timer = setInterval(() => {
console.log('start of interval');
this.submitService.checkIfJobRunning().subscribe(res => {
console.log("res from job running check is " + res);
this.jobFinished = res;
});
this.submitService.pollAuditTable().subscribe(res => {
console.log("res from audit table:\n\t" + res);
this.auditRows = res;
});
}, 10000); //runs every 10 seconds. will be longer after testing
console.log('entering while loop');
while (this.jobFinished === false) {
}
console.log('clearing interval, job should be finished now');
clearInterval(timer);
return;
}
setInterval中的console.log和其他东西从未被调用,我不明白为什么while循环会消耗执行并阻塞所有内容。如果要等到作业完成而不是whiledone{}循环,可以返回一个承诺:
returned() {
return new Promise((resolve, reject) => {
this.numReturned++;
if (this.numReturned !== this.numSubmitting) {
resolve();
return;
}
console.log('submit component line before jobstart call');
this.submitService.startJobToAuditTable().subscribe();
console.log('submit component line after jobstart call');
let timer = setInterval(() => {
console.log('start of interval');
this.submitService.checkIfJobRunning().subscribe(res => {
console.log("res from job running check is " + res);
this.jobFinished = res;
});
this.submitService.pollAuditTable().subscribe(res => {
console.log("res from audit table:\n\t" + res);
this.auditRows = res;
});
--> if(this.jobFinished === false) {
console.log('clearing interval, job should be finished now');
clearInterval(timer);
resolve();
}
}, 10000); //runs every 10 seconds. will be longer after testing
}
}
在异步代码中,使用resolve而不是return
稍后,当您调用此函数时,如果要等待作业完成,可以使用wait关键字
或者,如果不首选异步/等待:
returned().then(() => {
// code after all jobs done
})
删除此代码,它是阻塞的,不允许进程触发下一个勾号。因此,其他任何东西,包括设置间隔,都不会触发
// Delete this
while (this.jobFinished === false) { }
然后可以移动clearInterval
this.submitService.checkIfJobRunning().subscribe(res => {
console.log("res from job running check is " + res);
this.jobFinished = res;
if( this.jobFinished !== false ){
clearInterval(timer);
}
});
好吧,如果我理解你想正确地做什么,你可以这样做:
returned() {
this.numReturned++;
if (this.numReturned !== this.numSubmitting) {
return resolve();
}
console.log('submit component line before jobstart call');
this.submitService.startJobToAuditTable().subscribe();
console.log('submit component line after jobstart call');
let timer = setInterval(() => {
console.log('start of interval');
this.submitService.checkIfJobRunning().subscribe(res => {
console.log("res from job running check is " + res);
this.jobFinished = res;
if (this.jobFinished) {
console.log('clearing interval, job should be finished now');
clearInterval(timer);
}
});
this.submitService.pollAuditTable().subscribe(res => {
console.log("res from audit table:\n\t" + res);
this.auditRows = res;
});
}, 10000); //runs every 10 seconds. will be longer after testing
console.log('entering wait phase');
}
但是,您似乎希望函数在作业完成时返回,在这种情况下,您需要返回一个承诺并将其运行范围声明为async:
然后如何称呼它:
async someOtherMemberMethod() {
await this.returned();
// job is finished here
}
或者,如果不想将其放入异步函数中:
someOtherMemberMethod() {
this.returned().then(() => {
// job is finished here
});
}
这是一个异步调用,你的服务工作正常吗?你检查过你的无限循环是否阻塞了一切。不要这样做。setInterval创建对所提供函数的异步调用,前后两行之间没有执行暂停。根据10000参数,其他控制台日志将在10秒后显示。但是正如torazaburo提到的,你有一个无限循环,它阻止了其他任何事情的发生,因此这些事情无论如何都不会出现。是的,我第一次调用startJobToAuditTable工作正常,而应用程序其余部分的每一个调用都工作正常。好的,我的无限循环阻止了所有事情。正确的方法是什么,而不是无限循环?我希望间隔一直运行,直到this.jobFinished从间隔中的调用更改。
async someOtherMemberMethod() {
await this.returned();
// job is finished here
}
someOtherMemberMethod() {
this.returned().then(() => {
// job is finished here
});
}