views:

387

answers:

3

Hello,

So I have what amounts to an html form, but is not an actual <form>, per se. I have a 'Clear' button that I am currently using to reset all of the fields back to their defaults.

From an aesthetic standpoint, I would like for the form to fade out, reset while it's "gone", and fade back in completely reset. I've got this code so far to try to achieve this:

function Reset() {

    $formDiv.fadeOut(function() {

        // perform reset actions here
        $(this).fadeIn()
    });
}

However, what happens is, as the div is fading out, the fields get reset, so the user sees them all physically change back to their defaults while it's fading out. Then it fades back in as soon as it has finished fading out. Close, but I don't want the user to see the fields getting reset. I tried the following so it would wait until the fadeOut completed to reset the fields, but I got an infinite loop or something (the browser said the script was running slowly and asked me if I wanted to stop it):

function Reset() {

    $formDiv.fadeOut(function() {

        while (!$(this).is(':animated')) {
            // perform reset actions here
        }

        $(this).fadeIn()
    });
}

So I'm not really sure where to go from here. Any help would be appreciated. Thanks.

A: 

Look into .delay

More detail: Set the amount of time the animation will take for fadeOut(), and use a delay for the fadeIn() animation that is longer than the fadeOut animation time. In this example, the reset actions will happen during the fadeOut animation and presumably not take longer than 600 ms.

$formDiv.fadeOut(600,function() {

    // perform reset actions here
    $(this).delay(650).fadeIn()
});

Edit: Oops, misunderstood the problem slightly. You want the reset form logic to be trigger on the fadeIn() call, not the fadeOut. But I think it's still good to synchronize the animation events. Try something like:

$formDiv.fadeOut(600,function() {
    $(this).delay(650).fadeIn(function() {
        // perform reset actions here
    });
});
ghoppe
@ghoppe, Still a little off in your edit. Putting the reset code in the fadeIn() callback would have it fade back in, *then* do the reset code, which is the opposite of what I need. I need it to fade out, reset without the user seeing it get reset, then fade back in all reset. I think the delay could work, as the speed parameter suggested in the other answers isn't working. Maybe if I put the reset code on a delay that is slightly longer than the fadeOut duration? But then I'm not sure what I would assign the delay() to...
Mega Matt
Yes, that's why I set the delay to 650 and the fadeOut to 600, I presumed your reset actions would be fairly quickly executed. You may have to fiddle with the numbers. Alternatively, you may need to look at setTimeout() to trigger the reset actions and synchronize things. I haven't done any experimenting myself…
ghoppe
A: 

The callback really shouldn't fire until the animation has completed. Have you tried setting a speed on the fadeOut -- the documentation (which isn't always accurate) shows it as a required parameter and it just might be if you specify a callback -- i.e., it may be evaluating the function as the speed if you don't supply one explicitly.

$formDiv.fadeOut('fast', function() {
    // perform reset
    $(this).fadeIn();
});

EDIT: After looking through the code, it appears (in 1.3.2, at least) that if you supply a function as the first argument, it will evaluate it and use the return value as the speed. If you specify a speed, then your callback function it should work as you expect. The element should fade out, the callback will then fire and your reset and fade in code will be executed.

tvanfosson
I had hoped it would be that simple, but adding "normal" as the first parameter to fadeOut() still yields the same result. As it's fading out, the fields are reset. I agree with you, that the callback code should wait until the fade is finished (and I always thought that was the case), but that's just not happening.
Mega Matt
A: 

shouldn't you give a speed parameter before passing in the callback ?

oenli