Javascript node js app多次调用同一函数

Javascript node js app多次调用同一函数,javascript,node.js,Javascript,Node.js,好的,我正在开发一个全天候运行的应用程序。如果我这样写,我只有一个问题: setInterval(getWCProducts , 1000*intervall); function getWCProducts(){ //code } function test(){ console.log("Hello!") } test() function main() { console.log("doing

好的,我正在开发一个全天候运行的应用程序。如果我这样写,我只有一个问题:

    setInterval(getWCProducts  , 1000*intervall); 

    function getWCProducts(){
      //code
    }

    function test(){
        console.log("Hello!")
    }
    test() 
function main() {
     console.log("doing my main thing");
     setTimeout(main, 1000);
}

function setup() {
    console.log("doing my setup stuff ");
    main();
}

它会反复调用测试。它会在每秒钟打印“你好!”。为什么应用程序会这样运行?显然,“real”函数不仅仅是打印“hello!”。真正的是一个“设置功能”,它在应用程序开始循环之前准备好应用程序,我不能让它反复运行设置功能。

在完成设置之前不要启动循环

function main() {
     console.log("doing my main thing");
}

function setup() {
    console.log("doing my setup stuff ");
    setInterval(main, 1000);
}
我想指出的是,对于setInterval的大多数实际应用程序,您不希望以一个间隔启动函数调用,而是希望在上一次调用结束后以一个间隔启动每个函数。如果您是这样,修改后的代码将如下所示:

    setInterval(getWCProducts  , 1000*intervall); 

    function getWCProducts(){
      //code
    }

    function test(){
        console.log("Hello!")
    }
    test() 
function main() {
     console.log("doing my main thing");
     setTimeout(main, 1000);
}

function setup() {
    console.log("doing my setup stuff ");
    main();
}

如果您不发布“real”函数,或者至少是它的相关部分,那么没有人能够帮助您…我刚才发布的函数的行为与此相同。了解如何运行节点``function test(){console.trace(“Hello!”)}test()``来记录调用堆栈也会很有用