How to call .ajaxStart() on specific ajax calls
Asked Answered
E

10

56

I have some ajax calls on the document of a site that display or hide a progress bar depending on the ajax status

  $(document).ajaxStart(function(){ 
        $('#ajaxProgress').show(); 
    });
  $(document).ajaxStop(function(){ 
        $('#ajaxProgress').hide(); 
    });

I would like to basically overwirte these methods on other parts of the site where a lot of quick small ajax calls are made and do not need the progress bar popping in and out. I am trying to attach them to or insert them in other $.getJSON and $.ajax calls. I have tried chaining them but apparently that is no good.

$.getJSON().ajaxStart(function(){ 'kill preloader'});
Eros answered 28/7, 2009 at 1:23 Comment(1)
The Answer from 2012 will work for jquery 1.8+Susurrant
M
41

2018 NOTE: This answer is obsolete; feel free to propose an edit to this answer that will work.

You can bind the ajaxStart and ajaxStop using custom namespace:

$(document).bind("ajaxStart.mine", function() {
    $('#ajaxProgress').show();
});

$(document).bind("ajaxStop.mine", function() {
    $('#ajaxProgress').hide();
});

Then, in other parts of the site you'll be temporarily unbinding them before your .json calls:

$(document).unbind(".mine");

Got the idea from here while searching for an answer.

EDIT: I haven't had time to test it, alas.

Max answered 31/7, 2009 at 14:6 Comment(2)
If I use $(document).unbind(".mine"); before the JSON Call, how do I re-bind the namespace correctly afterwards?Insurgent
@nights this answer is from 2009. You can always suggest an alternative so I can edit accordingly.Max
W
27

There is an attribute in the options object .ajax() takes called global.

If set to false, it will not trigger the ajaxStart event for the call.

    $.ajax({
        url: "google.com",
        type: "GET",
        dataType: "json",
        cache: false,
        global: false, 
        success: function (data) {
Whitfield answered 15/7, 2017 at 14:37 Comment(0)
V
15

If you put this in your function that handles an ajax action it'll only bind itself when appropriate:

$('#yourDiv')
    .ajaxStart(function(){
        $("ResultsDiv").hide();
        $(this).show();
    })
    .ajaxStop(function(){
        $(this).hide();
        $(this).unbind("ajaxStart");
    });
Vellavelleity answered 2/12, 2010 at 19:5 Comment(2)
Thank you! I find your answer to be the easiest to implement.Helmand
Does this work? Can you use ajaxStart on your own selector? From documentation: As of jQuery 1.8, the .ajaxStart() method should only be attached to document.Littleton
H
14

Use local scoped Ajax Events

                success: function (jQxhr, errorCode, errorThrown) {
                    alert("Error : " + errorThrown);
                },
                beforeSend: function () {
                    $("#loading-image").show();
                },
                complete: function () {
                    $("#loading-image").hide();
                }
Hylotheism answered 9/2, 2017 at 1:32 Comment(0)
C
7

Furthermore, if you want to disable calls to .ajaxStart() and .ajaxStop(), you can set global option to false in your .ajax() requests ;)

See more here : How to call .ajaxStart() on specific ajax calls

Conscience answered 19/12, 2012 at 14:5 Comment(1)
this should be the correct answer. it enables you to stop the progress bar from showing for specific requests without having to change around all of your code.Intercalary
B
5

Unfortunately, ajaxStart event doesn't have any additional information which you can use to decide whether to show animation or not.

Anyway, here's one idea. In your ajaxStart method, why not start animation after say 200 milliseconds? If ajax requests complete in 200 milliseconds, you don't show any animation, otherwise you show the animation. Code may look something like:

var animationController = function animationController()
{
    var timeout = null;
    var delayBy = 200; //Number of milliseconds to wait before ajax animation starts.

    var pub = {};

    var actualAnimationStart = function actualAnimationStart()
    {
        $('#ajaxProgress').show();
    };

    var actualAnimationStop = function actualAnimationStop()
    {
        $('#ajaxProgress').hide();
    };

    pub.startAnimation = function animationController$startAnimation() 
    { 
        timeout = setTimeout(actualAnimationStart, delayBy);
    };

    pub.stopAnimation = function animationController$stopAnimation()
    {
        //If ajax call finishes before the timeout occurs, we wouldn't have 
        //shown any animation.
        clearTimeout(timeout);
        actualAnimationStop();
    }

    return pub;
}();


$(document).ready(
    function()
    {
        $(document).ajaxStart(animationController.startAnimation);
        $(document).ajaxStop(animationController.stopAnimation);
    }
 );
Burnard answered 28/7, 2009 at 1:46 Comment(0)
L
2

I have a solution. I set a global js variable called showloader (set as false by default). In any of the functions that you want to show the loader just set it to true before you make the ajax call.

function doAjaxThing()
{
    showloader=true;
    $.ajax({yaddayadda});
}

Then I have the following in my head section;

$(document).ajaxStart(function()
{
    if (showloader)
    {
        $('.loadingholder').fadeIn(200);
    }
});    

$(document).ajaxComplete(function() 
{
    $('.loadingholder').fadeOut(200);
    showloader=false;
});
Laceration answered 21/3, 2014 at 4:14 Comment(0)
V
2

use beforeSend or complete callback functions in ajax call like this way..... Live Example is here https://mcmap.net/q/82577/-jquery-should-i-use-multiple-ajaxstart-ajaxstop-handling

Source ShoutingCode

Vargo answered 22/1, 2016 at 6:32 Comment(0)
H
0

Use ajaxSend and ajaxComplete if you want to interspect the request before deciding what to do. See my reply here: https://mcmap.net/q/80476/-how-to-show-loading-spinner-in-jquery

Housemaster answered 2/4, 2013 at 11:53 Comment(0)
P
0
<div class="Local">Trigger</div>

<div class="result"></div>
<div class="log"></div>

$(document).ajaxStart(function() {
$( "log" )text( "Trigger Fire successfully." );
});

$( ".local" ).click(function() {
$( ".result" ).load("c:/refresh.html.");
});

Just Go through this example you get some idea.When the user clicks the element with class Local and the Ajax request is sent, the log message is displayed.

Preparatory answered 11/7, 2013 at 11:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.