jQuery延迟斗争

时间:2017-03-29 07:38:52

标签: jquery deferred

我努力想要按照我的意愿推迟工作。这是我的测试场景:



function myFn(val) {

  var d = $.Deferred();

  console.log("starting: " + val);

  setTimeout(function() {
    console.log('timeover:' + val);
    d.resolve();
  }, 1000);

  console.log("ending: " + val);
  return d.promise();
}

test = myFn(1).done(myFn(2));

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

输出

starting: 1
ending: 1
starting: 2
ending: 2
timeover: 1
timeover: 2

我想要的是:

starting: 1
timeover: 1
ending: 1
starting: 2
timeover: 2
ending: 2

我在哪里误解了延期?

0 个答案:

没有答案