0

I have the next setInterval in JS which emits the test() function only by the time the 5s ends:

let interval = setInterval(() => {
  test()
}, 5000)


function test() {
  console.log("Test") // will be emitted only in 5s
}

How can I tell the method to run the test() function without waiting?

Raz Buchnik
  • 7,753
  • 14
  • 53
  • 96
  • Possible duplicate of [Execute the setInterval function without delay the first time](https://stackoverflow.com/questions/6685396/execute-the-setinterval-function-without-delay-the-first-time) – sinaraheneba Apr 22 '19 at 09:58

2 Answers2

2

If I understand you right, you want the function to be executed immediately, and then every 5 seconds after that. In which case, just call it initially too.

let interval = setInterval(() => test(), 5000)
test();
Mitya
  • 33,629
  • 9
  • 60
  • 107
1
test(); //call initially and then after 5 sec
let intervalS = setInterval(() => {
  test()
}, 5000)


function test() {
  console.log("Test") // will be emitted only in 5s
}