views:

364

answers:

2

My method fades a div box out and has a callback that calls a method... like this:

function closeWindow(windowIdPrefix, speed) {
    $("#" + windowIdPrefix + "_ViewPanel").fadeOut(speed,
        function() {
            resetWindow(windowIdPrefix);
            numWindowsOpen--;
        }
    );
}

function resetWindow(windowIdPrefix) {
   alert("resetting window");
}

When this executes (onClick of a button) I have an alert in resetWindow to see how many times it executes.

It seems to execute forever, but I haven't sat there long enough closing the alert windows to find out.

I did some research and read in the Jquery Documentation:

callback (Optional) Function A function to be executed whenever the animation completes, executes once for each element animated against.

So I was wondering, even though I'm only fading out 1 div, does it count as 1.. plus 1 for each child element the div has?

Technically they are being animated because the inner elements are being faded out with the outer div, but if you watch the javascript in firebug only the outer div I'm fading out gets it's opacity/display changed.

If this is what's happening, how do I make sure the callback only gets executed once?

Edit: It was the line numWindowsOpen--; I hadn't defined numWindowsOpen before the function so for some reason that was making the call happen multiple times... Can anyone explain why this was happening?

A: 

The callback should only be invoked once for each element that matched the selector. Do you see multiple alerts when putting an alert message directly in the callback?

function() {
    alert('Test');
    resetWindow(windowIdPrefix);
    numWindowsOpen--;
}
Josh Stodola
Adding in alert('Test'); makes Test alert once (whether or not the fadeOut is in there). From what you've written (without fadeOut) resetWindow function only executes once. It's only when the fadeOut is in there that resetWindow gets executed multiple times
Matt
A: 

Problem was that numWindowsOpen wasn't declared before it was decremented there... Good reminder to make sure you look for simple things like that before going to more complicated tihngs.

Matt