2

I'm using the setInterval function in JavaScript but I get like one hundred reps per second from the console.log('NOW'). setTimeout also has the same effect.

Where is my mistake? I want to call the function "function1" every 15 minutes.

JavaScript Code:

   function1() {
      console.log('NOW');
      .
      .
      .
      });
    },
    refreshData() {
      this.function1();
      setInterval(this.refreshData(), 900000);
    },

Thanks in adavance!

3 Answers 3

3

you're invoking the function

setInterval(this.refreshData(), 900000);

rather than passing a reference to a function

setInterval(this.refreshData, 900000);
Sign up to request clarification or add additional context in comments.

Comments

0

Wrap your function call like This:

    var self = this;
    refreshData() {
      this.function1();
      setInterval(function(){  self.refreshData() }, 900000);
    }

Comments

0

There are two possible ways:

In provided code you should use setTimeout, because you restart function manually:

function function1() {
  console.log('NOW');
}

function refreshData() {
  this.function1();
  setTimeout(this.refreshData, 3000);
}

refreshData();

Or simply replace existing logic with setInterval, it should do all the job you've implemented manually:

function function1() {
  console.log('NOW');
}

this.function1();
setInterval(this.function1, 3000);

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.