Loop over an array with the given timeout for each element


Problem

You are given an array of objects each with its name and timeout value. Iterate over each element inside an array in the interval of the given timeout.

Solution

  • We have to set timeout while iterating over an element
  • Once the timeout completes, we have to recurse over the given next item in the array
  • Increment the counter for the current element so in the recursion we can recurse over the next element

Code

Outputs the following:

event1 Started.
event1 Ended. // at-least after 1s
event2 Started.
event2 Ended. // at-least after 2s
event3 Started.
event3 Ended. // at-least after 3s

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Show Buttons
Hide Buttons