Why doesn't Jquery fadeOut() work? Hide() does

Hans picture Hans · Nov 26, 2013 · Viewed 10.7k times · Source

After an Ajax result I'm trying to fadeOut a html button. This works most of the time, however sometimes the button isn't faded out and I can't figure out why.

$.ajax({
    type: frm.attr('method'),
    url: frm.attr('action')+'?time='+timestamp,
    data: frm.serialize(),
    dataType: 'json',
    success: function (data) {
        if(data['success'] === true){
            hideSaveButton();
        }
    }
});

function hideSaveButton(){
    $('#saveBtn').fadeOut(250);
}

Accessing the hideSaveButton() function doesn't seem to be a problem. It's just the fadeOut() function that isn't working. I tried replacing fadeOut() by hide() which works without any problem.

Any ideas?

Answer

Abhitalks picture Abhitalks · Nov 26, 2013

It seems that the calls are being made in quick succession. Which means that the fadeOut fires before the earlier one has finished.

You may want to stop() before you attempt the fadeOut():

$('#saveBtn').stop().fadeOut(250);

More info: http://api.jquery.com/stop/