0

I want to insert array values after setTimeout() executes. At the beginning default value must be setTimeout(msg, 60000) after that, I want to insert arr values one by one after execution.

For example:

This is my Array arr=[2, 5, 8] at the beginning setTimeout(msg, 60000) after execution setTimeout(msg, arr[0]) after second execution setTimeout(msg, arr[1]) so on I need to add arr elements to the function. Please can I know solution for this problem?

1
  • 2
    Don't scream! Commented Nov 2, 2018 at 7:59

1 Answer 1

1

Just use a recursive timer:

  const times = [2, 5, 8];

  function next() {
    msg();
    if(times.length) setTimeout(next, times.shift() * 1000);
  }

 setTimeout(next, 6000);
Sign up to request clarification or add additional context in comments.

6 Comments

I was thinking he was wanting something like that, but this bit after that i want to insert arr valuse one by one after execution. got me confused...
@keith I guess he wants to insert arr[0] into setTimeout and so on, thats how I understood it
add the code to the question @sd_dewasurendra. it will make it easier for others to find a specific or generic solution to the problem at hand.
@sd_dewasurendra no worries :)
@rai no! answers go into the answer section!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.