I need just one callback at the and of multiple callbacks. With a simple “class” MultiCallback that count the calls it seems to be solved.
But the fadeIn function of jQuery calls mCallback just once.
It have to call mCallback 3 times. That happens by using a callback function directly inside top scope.
Tested my MultiCallback class also on my own functions without any problems. I know
there a other ways like Hide multiple elements with jQuery and get one callback…
Just wanna know what is wrong with my logic.
<div id="1" style="display: none; background-color: green;"></div>
<div id="2" style="display: none; background-color: red;"></div>
<div id="3" style="display: none; background-color: blue;"></div>
<script>
var allElements = "#1, #2, #3";
var multiCallback = new MultiCallback( $(allElements).length, function()
{
console.log("NEVER ENTER CALLBACK");
});
$(allElements).fadeIn(400, multiCallback.mCallback());
function MultiCallback(limit, fn)
{
var finishedCalls = 0;
function mCallback()
{
// Just enter one time !
if (++finishedCalls == limit)
{
fn();
}
}
return {
mCallback : mCallback
}
}
</script>
Change
to