0

I've got a question, how can I handle the following situation without the needing of array.length different variables?

for (var i = 0; i <= arrayOfStrings.length; i++) {
    if (i % 3 == 0 && i != 0) {
        var name = i - 3;
        var category = i - 2;
        var searchItem = i - 1;
        setTimeout(function () {
            amazonProductAPI.searchItemAndPersist(arrayOfStrings[name], arrayOfStrings[category], arrayOfStrings[searchItem])
        }, time);
        time = time + 5000;
    }
}

I want that the function is called after 5 seconds plus 5 seconds per iteration.

Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
Niklas
  • 45
  • 2
  • 7
  • Use a function, and you get your different variables for free by calling the function multiple times. – Bergi Apr 24 '16 at 15:03
  • Please fix your indentation, your code is really hard to read. – Bergi Apr 24 '16 at 15:03
  • The solution linked is a bad solution to this particular problem. async.js would make sense here and not in the solution linked. This question should be reopened to or linked to a better answer. – Stefan Kendall Apr 24 '16 at 15:08
  • I already tried async with the "series" function, maybe someone can tell me how it is used in this situation? That would be great" – Niklas Apr 24 '16 at 15:11

0 Answers0