Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I add a delay inside each iteration of an _.each loop in underscore.js?

How can I add a delay inside each iteration of an _.each loop to space out the calling of an interior function by 1 second?

  _.each(this.rows, function (row, i) {
      row.setChars(msg[i] ? msg[i] : ' ');
  });
like image 760
stroz Avatar asked Jul 01 '14 03:07

stroz


1 Answers

You don't need extra IIFE

_.each(this.rows, function (row, i) {
    setTimeout(function () {
        row.setChars(msg[i] ? msg[i] : ' ');
    }, 1000 * i);
});

since you're not doing it in an explicit for loop.

like image 71
zerkms Avatar answered Sep 21 '22 01:09

zerkms