0

I am trying to figure out a way of how to loop through an array, do something with that array[index] pause function for x seconds and move to next index in that array.

That is what I achieved so far. It prints out the whole array, but I need it to print out only one value, do something with it, then proceed to the next one and so on.

var destinations = ['Greece', 'Maldives', 'Croatia', 'Spain'];

var index = 0;

for (index = 0; index < destinations.length; index++){
  console.log(destinations[index]);
};

3
  • 1
    So... where are you stuck? What are you trying to do? What problems did you run into? Commented Feb 20, 2019 at 18:40
  • @tymeJV I need to loop through this array, grab 1 value at the time, use it, then grab the next one and so on. On top of that I need to pause this function each time it grabs a value, so it is not spitting all of the results at once, but one after the other Commented Feb 20, 2019 at 18:43
  • Have a look at the forEach function there is great documention on mdn var destinations = ['Greece', 'Maldives', 'Croatia', 'Spain']; destinations.forEach(function(element, i) { // do something to each value console.log(element, i); }); Commented Feb 20, 2019 at 18:47

3 Answers 3

2

You could take the iteration protocols with an implementation of Symbol.iterator in Array#[@@iterator]() and iterate until no more elements are available.

var destinations = ['Greece', 'Maldives', 'Croatia', 'Spain'],
    gen = destinations[Symbol.iterator]();
    interval = setInterval(function () {
        var g = gen.next();
        if (g.done) {
            clearInterval(interval);
            return;
        }
        console.log(g.value);
    }, 1000);

Sign up to request clarification or add additional context in comments.

Comments

0

You can use setTimeout to do this.

var time_between_steps = 1000
var destinations = ['Greece', 'Maldives', 'Croatia', 'Spain']
var index = 0

function nextItem(){
  // do things here with destinations[index]
  console.log(destinations[index])
  
  index++
  
  // if we have not yet reached the end of the array, run nextItem again after time_between_steps
  if(index<=destinations.length-1)
    setTimeout(nextItem,time_between_steps)
}


nextItem()

Comments

0

setTimeout can be used here:

Basically, you can define a method processItem and call it with the current parameter. Also the delay can be set with a variable.

After the delay, the method is called with a parameter.

var delay = 1000; // 1 sec
var destinations = ['Greece', 'Maldives', 'Croatia', 'Spain'];
var index = 0;

function processItem(item){
  console.log("Item " + item);
  // do stuff
}

function iterate(index){
  processItem(destinations[index]);
  index++;
  if (index < destinations.length){
   setTimeout(iterate, delay, index);
  }
}

iterate(index);

Comments

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.