0

Okay, so i have developing an application which will run 24/7. I only have one problem, if I write like this:

    setInterval(getWCProducts  , 1000*intervall); 

    function getWCProducts(){
      //code
    }

    function test(){
        console.log("Hello!")
    }
    test() 

It will call test over and over again. It will print "Hello!" about every each secound. Why is the app behaving like this? Obviously the "real" function isn't just going to print "hello!". The real one is a "setup-function" which prepares the app before it starts looping and I can't have it running the setup function over and over again .

4
  • 3
    If you don't post the "real" function, or at least a relevant portion of it, no one is going to be able to help you... Commented Mar 30, 2017 at 8:21
  • The function I just posted behaivs the same way. Commented Mar 30, 2017 at 8:22
  • it would also be useful to know how you're running node Commented Mar 30, 2017 at 8:22
  • ``` function test(){ console.trace("Hello!") } test() ``` to log a call stack Commented Mar 30, 2017 at 8:26

1 Answer 1

1

Just don't start the loop until you complete the setup.

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

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

I'd like to point out that for most real-world applications of setInterval, you don't want to start the function calls one Interval apart, you really want to start each function one Interval after the previous call ended. If that's the case for you the modified code would look like this:

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

function setup() {
    console.log("doing my setup stuff ");
    main();
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.