7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

jQuery.Deferred で並列と直列を混ぜる

Posted at

foo -> [bar, baz] -> foo -> [bar, baz] -> foo の順で実行します。[bar, baz] は並列に実行。

なお、説明のため jQuery.animate()queue は無効にしておきます。

$el = $('body')
foo = ->
  $el.animate { marginTop: 100, marginLeft: 0 }, { duration: 1000, queue: false }
bar = ->
  $el.animate { marginLeft: 100 }, { duration: 1000, queue: false }
baz = ->
  $el.animate { marginTop: 0 }, { duration: 1000, queue: false }

$.Deferred( (dfd) ->
  dfd
    .pipe(foo)
    .pipe( -> $.when bar(), baz() )
    .pipe(foo)
    .pipe( -> $.when bar(), baz() )
    .done(foo)
).resolve()
var $el = $('body');

function foo() {
  return $el.animate({ marginTop: 100, marginLeft: 0 }, { duration: 1000, queue: false });
}
function bar() {
  return $el.animate({ marginLeft: 100 }, { duration: 1000, queue: false });
}
function baz() {
  return $el.animate({ marginTop: 0 }, { duration: 1000, queue: false });
}

$.Deferred(function (dfd) {
  dfd
    .pipe(foo)
    .pipe(function() {
      return $.when(bar(), baz());
    })
    .pipe(foo)
    .pipe(function() {
      return $.when(bar(), baz());
    })
    .done(foo)
}).resolve();

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?