简体   繁体   English

在请求运行时阻止jQuery函数运行

[英]Prevent jQuery function from running while request is running

I have a function that I need to reuse often. 我有一个需要经常重用的功能。 The problem occurs when I load another page into the current page, the function fires twice. 当我将另一个页面加载到当前页面时,会发生问题,该函数将触发两次。 I tried this solution but could not get it to work ( Disable jquery function after 1 click, to prevent multiple executions ). 我尝试了此解决方案,但无法使其正常工作( 单击1次后禁用jquery函数,以防止多次执行 )。 Any solutions to prevent the function from firing multiple times while a request is being made? 有什么解决方案可以防止在发出请求时多次触发该功能? The function should only fire once per click. 每次点击仅触发一次功能。

(function($) {
    $.fn.likeclick = function(thiss, key) {  
        var $this = thiss;
        var pid=key;
        if ($this.hasClass('addd')) {
            $.post("/blah.php?id="+pid, function(data) {
                if (data=="ok"){
                    $this.removeClass('addd').addClass('rem');
                    $('.ccount#'+pid).html(function(i,val) {
                        return (parseFloat(val) + 1).toFixed(0);
                    });
                }
            });
        } else {
            $.post("/blah1.php?id="+pid, function(data) {
                if (data=="okk"){
                    $this.removeClass('rem').addClass('addd');
                    $('.ccount#'+pid).html(function(i,val) {
                        return (parseFloat(val) - 1).toFixed(0);
                    });
                }
            });
        }
    };
})(jQuery);

Usage: 用法:

<div class="like addd"></div>

$('.like').click(function() {
    likeclick($(this),this.id);
});

If I'm understanding your question correctly, what you need is a way for the function to know that if there is currently any active requests before making its request. 如果我正确地理解了您的问题,那么您需要的是一种让函数知道在发出请求之前当前是否有活动请求的方式。 Why not add a boolean like hasActiveRequest to keep track of this? 为什么不添加类似hasActiveRequest的布尔值来跟踪此情况? So by default its false , then when they click it, check that boolean...if false then set it to true and make the request; 因此,默认情况下为false ,然后单击它们时,检查布尔值...如果为false,则将其设置为true并发出请求; else if its true , then don't make request (since there is already one in progress). 否则,如果它为true ,则不要发出请求(因为已经在进行请求)。 When the request is complete, then set the boolean back to false . 请求完成后,将布尔值设置回false

Since the functions did not work on the pages (or data) that were loaded with the .post() method, I tried adding the jQuery function to the loaded page directly or with a .js file. 由于这些功能不适用于使用.post()方法加载的页面(或数据),因此我尝试将jQuery函数直接或通过.js文件添加至加载的页面。 Either way the code was being executed multiple times after a few pages were loaded using .post(). 无论哪种方式,在使用.post()加载几页后,代码都会多次执行。 Whether I used delays, timeout functions or added booleans to try to prevent the function from being fired multiple times per click, the function still executed multiple times. 无论我使用延迟,超时函数还是添加布尔值来尝试防止每次单击均触发该函数多次,该函数仍会多次执行。

After an endless search I came across this article ( http://docs.jquery.com/FAQ#Why_do_my_events_stop_working_after_an_AJAX_request.3F ) in the jQuery documentation. 经过无休止的搜索,我在jQuery文档中找到了这篇文章( http://docs.jquery.com/FAQ#Why_do_my_events_stop_working_after_an_AJAX_request.3F )。

In that article it states: 在该文章中指出:

Beware! 谨防! As of jQuery 1.4.2, binding the same handler to the same element multiple times will cause it to execute more than once. 从jQuery 1.4.2开始,多次将同一处理程序绑定到同一元素将导致其执行多次。 This differs from previous versions of jQuery as well as the DOM 2 Events spec (which normally ignores duplicate event handlers). 这与jQuery的早期版本以及DOM 2 Events规范(通常会忽略重复的事件处理程序)不同。

More research turned up the .live() method, but that method is deprecated. 更多研究提出了.live()方法,但该方法已被弃用。 The .on() method replaced .live(). .on()方法替换了.live()。 So my solution is: 所以我的解决方案是:

$(document).on('click', '.like', function(){
var $this = $(this);
likeclick($this,this.id);
});

Now I don't have to add functions to the loaded page (data) from the .post() method or use other methods to try and prevent the function from firing twice. 现在,我不必从.post()方法向加载的页面(数据)添加函数,也不必使用其他方法来尝试防止函数触发两次。

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

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