Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define setInterval function [duplicate]

Tags:

javascript

I'm using V8 in conjuction with c++ and native window setInterval functon is not defined.

What would be an algorythm to create something like native setInterval but in pure js?

like image 839
Denis Matafonov Avatar asked Dec 12 '25 14:12

Denis Matafonov


1 Answers

Assuming setTimeout is available (not probable, but you did not specify that):

function setInterval(fn, t) {
  let id = {};

  function wrapper() {
    id.timeout = setTimeout(wrapper, t);
    fn.apply(this, arguments);
  }

  id.timeout = setTimeout(wrapper, t);

  return id;
}

function clearInterval(id) {
  clearTimeout(id.timeout);
}
like image 90
Tobias Avatar answered Dec 15 '25 08:12

Tobias



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!