如何在特定的 ajax 调用上调用 .ajaxStart()

发布于 2024-07-28 00:07:17 字数 445 浏览 4 评论 0原文

我对网站的文档进行了一些ajax调用,这些调用根据ajax状态显示或隐藏进度条

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

我想基本上在网站的其他部分覆盖这些方法,在这些地方进行大量快速的小型ajax调用,但不这样做需要进度条弹出和弹出。 我正在尝试将它们附加到或插入到其他 $.getJSON 和 $.ajax 调用中。 我尝试过将它们链接起来,但显然这不好。

$.getJSON().ajaxStart(function(){ 'kill preloader'});

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'});

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

々眼睛长脚气 2024-08-04 00:07:19

如果您想在决定做什么之前检查请求,请使用 ajaxSend 和 ajaxComplete。 请参阅我的回复:https://stackoverflow.com/a/15763341/117268

Use ajaxSend and ajaxComplete if you want to interspect the request before deciding what to do. See my reply here: https://stackoverflow.com/a/15763341/117268

梦开始←不甜 2024-08-04 00:07:19
<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.");
});

通过这个例子你就会得到一些想法。当用户单击类为 Local 的元素并且发送 Ajax 请求时,将显示日志消息。

<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.

夜巴黎 2024-08-04 00:07:18

如果您将其放入处理 ajax 操作的函数中,它只会在适当的时候绑定自身:

$('#yourDiv')
    .ajaxStart(function(){
        $("ResultsDiv").hide();
        $(this).show();
    })
    .ajaxStop(function(){
        $(this).hide();
        $(this).unbind("ajaxStart");
    });

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");
    });
恋竹姑娘 2024-08-04 00:07:18

使用本地范围的 Ajax 事件

                success: function (jQxhr, errorCode, errorThrown) {
                    alert("Error : " + errorThrown);
                },
                beforeSend: function () {
                    $("#loading-image").show();
                },
                complete: function () {
                    $("#loading-image").hide();
                }

Use local scoped Ajax Events

                success: function (jQxhr, errorCode, errorThrown) {
                    alert("Error : " + errorThrown);
                },
                beforeSend: function () {
                    $("#loading-image").show();
                },
                complete: function () {
                    $("#loading-image").hide();
                }
后来的我们 2024-08-04 00:07:18

此外,如果您想禁用调用.ajaxStart().ajaxStop(),您可以设置global.ajax() 请求中选择 false ;)

在此处查看更多信息:如何在特定的 ajax 调用上调用 .ajaxStart()

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

多情癖 2024-08-04 00:07:18

不幸的是,ajaxStart 事件没有任何可用于决定是否显示动画的附加信息。

无论如何,这是一个想法。 在你的 ajaxStart 方法中,为什么不在 200 毫秒后开始动画? 如果 ajax 请求在 200 毫秒内完成,则不显示任何动画,否则显示动画。 代码可能类似于:

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);
    }
 );

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);
    }
 );
残龙傲雪 2024-08-04 00:07:18

我有一个解决方案。
我设置了一个名为 showloader 的全局 js 变量(默认设置为 false)。 在您想要显示加载程序的任何函数中,只需在进行 ajax 调用之前将其设置为 true 即可。

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

然后我的头部有以下内容;

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

$(document).ajaxComplete(function() 
{
    $('.loadingholder').fadeOut(200);
    showloader=false;
});

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;
});
囍孤女 2024-08-04 00:07:18

像这样在ajax调用中使用beforeSend或完成回调函数......
现场示例在这里
https://stackoverflow.com/a/34940340/5361795

来源ShoutingCode

use beforeSend or complete callback functions in ajax call like this way.....
Live Example is here
https://stackoverflow.com/a/34940340/5361795

Source ShoutingCode

七色彩虹 2024-08-04 00:07:17

2018 注意:此答案已过时; 请随意提出对此答案的可行修改。

您可以使用自定义命名空间绑定 ajaxStart 和 ajaxStop:

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

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

然后,在网站的其他部分,您将在 .json 调用之前暂时解除它们的绑定:

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

此处,同时搜索答案。

编辑:我还没有时间测试它,唉。

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.

霞映澄塘 2024-08-04 00:07:17

选项对象 .ajax() 中有一个名为 global 的属性。

如果设置为false,则不会触发调用的ajaxStart事件。

    $.ajax({
        url: "google.com",
        type: "GET",
        dataType: "json",
        cache: false,
        global: false, 
        success: function (data) {

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) {
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文