node.js - setTimeout inside forloop? JS NODEJS -


i'm trying create delay between every loop. i'm ending delay before loop being fired. way doing following:

    (var = 0; < items.length; i++)     {       settimeout(function()       {         console.log(i + ". " + items[i]['name']);         pricemanagement.findprices(items[i]['name']);       }, 3000);     } 

you can this:

 (var = 0; < items.length; i++)     {       settimeout(function(i)       {         console.log(i + ". " + items[i]['name']);         pricemanagement.findprices(items[i]['name']);       }, 3000*i, i);     } 

or can pass items[i]['name'] parameter well:

 (var = 0; < items.length; i++)     {       settimeout(function(i, name)       {         console.log(i + ". " + name);         pricemanagement.findprices(name);       }, 3000*i, i, items[i]['name']);     } 

and recommend read this: http://javascriptissexy.com/understand-javascript-closures-with-ease/


Comments