【发布时间】:2021-10-20 22:08:34
【问题描述】:
我正在寻找在 nodejs 上使用 express 执行一个函数的确切时间,如果时间已经过去但函数没有结束,则函数停止。
我尝试使用 setTimeOut。但它只适用于在“x”时间之后执行一个函数。我只需要执行“x”时间的功能。
【问题讨论】:
标签: javascript node.js express
我正在寻找在 nodejs 上使用 express 执行一个函数的确切时间,如果时间已经过去但函数没有结束,则函数停止。
我尝试使用 setTimeOut。但它只适用于在“x”时间之后执行一个函数。我只需要执行“x”时间的功能。
【问题讨论】:
标签: javascript node.js express
您可以设置一个布尔变量并在“x”时间后更改其值,同时检查函数中的相同变量。
var Continue = true; // Our Boolean variable.
var x = 5000; // Execute the code for amount of milliseconds.
function canContinue(){
return Continue;
}
var ourInterval = setInterval( function () {
if(canContinue())
console.log("Continue the code for one more second.")
else
clearInterval( ourInterval )
}, 1000 );
setTimeout( function () {
Continue = false;
}, x);
希望对您有所帮助。
【讨论】: