承诺和延期不起作用,在结束前完成

时间:2017-05-18 12:01:40

标签: javascript jquery

var promises = J_lis.each(function(i){
    // Deferred from promises
    var dfd = $.Deferred();
    var setSrc = this.src;
    var setId = this.id;

    setTimeout(function() {
      // Looping thought image
      that._loadCanvasImage(setId, setSrc);
      // Resolve
      dfd.resolve();
    }, (i+1)*5000);

    // Return promise
    return dfd.promise();
}).get();

// Get when loop each is finished
  $.when.apply($, promises).then(function()
  {
    console.log("done");
  });

我的意图是循环使用setTimeout逐个图像,一次是comfire完成循环所有图像,它移动另一个函数,我使用来自js的承诺。

我的功能在所有图片循环播放之前,从 console.log 显示完成

我的代码出了什么问题?我如何确保所有图像循环,然后传递给then函数?

1 个答案:

答案 0 :(得分:0)

.each()将返回J_lis

您想使用.map()

var promises = J_lis.map(function () {
   // return a promise here.
}).get();
相关问题