简体   繁体   English

mockjax loader - 停止运行特定功能

[英]mockjax loader - stop from running for specific function

I am making use of Jonathan Sampson's answer for my jquery busy loader. 我正在利用Jonathan Sampson对我的jquery busy loader的回答 it works 100% and detects any jquery posting or the like and shows the loader. 它100%工作并检测任何jquery发布等,并显示加载器。

my problem is that most times I want the user to wait when I fetch info from the database so happy for the loader to appear. 我的问题是,大多数情况下,当我从数据库中获取信息时,我希望用户等待,以便加载器显示。

What I want however, is for certain functions only for the loader not to show when I save info to the database. 但是,我想要的是,只有当我将信息保存到数据库时才能显示加载程序的某些功能。

the fiddle can be found here 小提琴可以在这里找到

for example, the below causes the loader to run. 例如,下面导致加载程序运行。 how can I modify this so mockjax knows not to run for this function only? 我如何修改这个,以便mockjax知道不运行此功能?

$(document).on("click", function(){
    $.get("/mockjax");        
});

Thanks for the help as always. 一如既往地感谢您的帮助。

Short version (with code) 短版(带代码)

Click the heading - Animation 单击标题 - 动画

Click the paragraph - No Animation 单击段落 - 无动画

http://jsfiddle.net/hPS2v/ http://jsfiddle.net/hPS2v/

$.ajax({
    url: "/mockjax",
    type: "GET",
    global: false //This is the key
});

-- -

The slightly longer version 稍长的版本

There is a great variable available in the $.ajax() method which let's you stop ajax events from firing (but not the ajax itself). $ .ajax()方法中有一个很好的变量可以让你停止ajax事件的触发(但不是ajax本身)。 This is called global . 这称为全球性 From the docs 来自文档

global (default: true) 全局 (默认值:true)

Type: Boolean 类型:布尔值

Whether to trigger global Ajax event handlers for this request. 是否为此请求触发全局Ajax事件处理程序。 The default is true. 默认值为true。 Set to false to prevent the global handlers like ajaxStart or ajaxStop from being triggered. 设置为false以防止触发像ajaxStart或ajaxStop这样的全局处理程序。 This can be used to control various Ajax Events. 这可用于控制各种Ajax事件。

With this in mind, I have slightly changed your code a little to demonstrate how we can make this work. 考虑到这一点,我略微改变了你的代码,以展示我们如何使这项工作。

1) Moved the AJAX call into a function with 2 parameters: URL and eventTrigger. 1)将AJAX调用移动到具有2个参数的函数中:URL和eventTrigger。 Basically we want the URL to be "/mockajax", and the eventTrigger to be true or false (depending on if and when you want to show your animation) 基本上我们希望URL为“/ mockajax”,并且eventTrigger为true或false(取决于是否以及何时显示动画)

2) I took the event handler off the $(document) and added 2 events to the heading tags and the p tags so I can demonstrate the AJAX working in 2 places with the same code (1 with and 1 without an animation) 2)我从$(文档)中取出了事件处理程序,并在标题标签和p标签中添加了2个事件,因此我可以使用相同的代码(1个带1和1个没有动画)演示AJAX在2个位置工作

Hope this helps! 希望这可以帮助!

You can use a variable as a flag which denotes your preference of not showing the loader. 您可以使用变量作为标志,表示您不显示加载程序的首选项。

//let say, showLoader, by default true
var showLoader = true;

Now, add this to your ajax setup as required. 现在,根据需要将其添加到您的ajax设置中。

$(document).on({
    ajaxStart: function () {
        // if showLoader true, then shows loading image
        if (showLoader)
            $body.addClass("loading");
    },
    ajaxStop: function () {
        $body.removeClass("loading");
        // set showLoader to true
        showLoader = true;
    }
});

Change showLoader state as required by you. 根据您的要求更改showLoader状态。

$(document).on("click", function (e) {
    // if target element is anchor tag, then do not show loading image - example
    if (e.target.tagName === 'A') {
        showLoader = false;
    }
    $.get("/mockjax");
});

JSFiddle: http://jsfiddle.net/VpDUG/5177/ JSFiddle: http//jsfiddle.net/VpDUG/5177/

Hope, it helps. 希望能帮助到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM