Thursday, 9 February 2017

Alterative for javascript interval



Hello folks. If you face a scenario you where you cannot use JavaScript interval or facing some problem because of interval then following alternative can be considered.

The below example makes use of date for making interval.

  • Here we create a function waitAndExecute.  
  • A time duration in milliseconds is provided as parameter to this function. 
  • A variable start is set as current time of the day
  • Another variable end is set as start + input duration
  • Now while loop executes till the end is equal to start
  • Just to check the functionality current daytime is logged in console after the loop
  • The same function is called again after the loop to make it recursive
Invoking function with 3 seconds 
(3000 milliseconds):

waitAndExecute(3000);