Is this code looping or recursive?

var timer1 = (cb, time) => {
  (function loop() {
    cb();
    setTimeout(loop, time);
  })();
};

var timer2 = (cb, time) => {
  cb();
  setInterval(cb, time);
};
    Is the
  1. timer1 code looping or recursive?
  2. what"s the difference between timer1 and timer2?
Jul.10,2022

Recursive, indirect.
the difference is that 1 stops when cb throws an exception and 2 doesn't stop.
another difference is that 1 can be transformed into high precision, while 2 depends entirely on the accuracy of the browser itself


  1. from a code point of view, calling yourself is recursive, but with an extra interval
  2. The difference between timer1 and timer2 is actually the difference between setTimeout and setInterval
  3. .
setTimeout()
setTimeout()

setInterval()
Menu