-1

I would like to re run a the same function after x amount of seconds. the X changes every time the function is ran like the example i have provided below but the code below does not wait half a second before fireing the function again, it just instantly runs the function in an infinite loop

test();
var interval = 500
function test() {
    interval = interval + 500;
    console.log("1");
    setTimeout(test(), interval);
}
lizart
  • 127
  • 2
  • 2
  • 8

1 Answers1

2

Change:

setTimeout(test(), interval);

to

setTimeout(test, interval);

test() calls the function immediately.

j08691
  • 204,283
  • 31
  • 260
  • 272