Search code examples
javascriptiosreact-nativenstimer

NSTimer equivalent for Javascript


Someone please suggest an equivalent for NSTimer in Javascript, here is my requirement.

  • Want to run a piece of code after a time interval.
  • Control to cancel execution at any point(Like invalidate timer in NSTimer).
  • Reset timer interval.

Solution

  • You can use Javascript's setInterval()/setTimeout() Methods to run the piece of code.

    You can invalidate using clearTimeout()/clearInterval()

    You can visit the following page from W3Schools to get more info in detail: https://www.w3schools.com/js/js_timing.asp