Stop ajax loader while closing popup

While middle of ajax request(showing spinner) closed the fancybox window, again opened still spinner is showing, how can i refresh it.

<div id="myBtn">Click here</div>
<div class="loader" style="display:none"><img src="path" /></div>

$('#myBtn').click(function() {
    $.fancybox({
        type: 'inline',
        content: '#content'
    });
});

function results(){

$.ajax{

	type: 'POST',
   	 url: url,
   	 data: data,	

	success:function(data){
	//for loop to get json data
	},
	beforeSend:function(){
	$('.loader').css('display','block');
	},
	complete: function(){ $('.loader').css('display','none');}
}

First of all, there’s no spinner in the fancybox, just the string “Content”. Assuming you have that .loader div in the fancybox, have you tried the afterClose callback? Like

$('#myBtn').click(function() {
    $.fancybox({
        type: 'inline',

        // Clone the content from a certain element
        // which includes the .loader div
        content: $('.content').clone(),

        // Always hide the .loader when the popup
        // got closed
        afterClose: function() {
            $('.loader').hide();
        }
    });
});

Also, it would be advisable to actually abort the AJAX request when you’re dismissing the response anyway, like

var req;

$('#myBtn').click(function() {
    $.fancybox({
        type: 'inline',
        content: $('content').clone(),
        afterClose: function() {
            $('.loader').hide();

            if (req) {
                req.abort();
            }
        }
    });
});

// Then, later
req = $.ajax({ /* etc. */});

While typing the above I also noticed that the parentheses of the AJAX call are missing in your snippet. It would really be helpful if you posted code examples as they are supposed to work, i.e. that do not contain additional bugs and syntax errors we’d have to fix first if we wanted to run your code in a fiddle or something. ;-)

Thanks for the quick solution. Going forward I will make sure my snippet should be error free… :wink:

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.