Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

The question is pretty straightforward. If I select two or more elements with jQuery and, for example, use jQuery's fadeOut() function to hide them, the callback function is invoked twice (for each element). Is there a way to only receive one callback?

The code I am currently using to perform this task is pasted below.

$('#element-1, #element-2').fadeOut( 250, function() { /* Callback invoked twice. */ });

A similar question has been posted before (jQuery multiple animate() callback), but the solution seems quite complicated for what seems a simple problem.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
323 views
Welcome To Ask or Share your Answers For Others

1 Answer

You can use $.when [docs] (deferred objects):

$.when($('#element-1, #element-2').fadeOut(250)).then(function() {
    // do something
});

DEMO

This works with any animation afaik.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...