首页 > 解决方案 > Node.js : 在后台运行函数

问题描述

我需要在后台运行该功能

function bk () {
    console.log("Function Start");
    var i;
    for (i = 0; i < 10000000000; i++) {
        y = 0
    }
    console.log("Function END");
}

console.log("MAIN START");
bk()
console.log("MAIN end");

目前的输出是:

MAIN START
Function Start
Function END
MAIN end

所需的输出是:

MAIN START
MAIN end
Function Start
Function END

或者

MAIN START
Function Start
MAIN end
Function END

想法是 JS 不应该等待函数结束打印“MAIN END”

标签: javascriptnode.js

解决方案


这应该这样做: -

console.log("MAIN START");
setTimeout(bk,0);
console.log("MAIN end");

推荐阅读