简体   繁体   中英

How to add setTimeout function for the Foreach loop

Hi want to set the timeout funtion for the Freach loop for the callback i am setting the timeout funtion. but its displaying Undifined error. can anybody Explain me here is My code.

 function async(array,cb){ array.forEach(function () { setTimeout(cb,0); }) } async([1,2,3,4],function(i){ console.log(i); }); 

Try:

 function async(array,cb){ array.forEach(function(e) { setTimeout(function() { cb(e); },0); }); } async([1,2,3,4,5], function(i) { console.log(i); }); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM