简体   繁体   English

带有getJSON的ajaxComplete导致循环

[英]ajaxComplete with getJSON causing loop

I am using ajaxComplete to run some functions after dynamic content is loaded to the DOM. 在将动态内容加载到DOM之后,我正在使用ajaxComplete运行一些功能。 I have two separate functions inside ajaxComplete which uses getJSON. 我在使用getJSON的ajaxComplete内部有两个单独的函数。

  • Running any of the functions once works fine 运行任何功能一次都可以正常工作
  • Running any of them a second time causes a loop cause they are using getJSON. 第二次运行它们中的任何一个都会导致循环,原因是它们正在使用getJSON。

How do I get around this? 我该如何解决?

I'm attaching a small part of the code. 我只附加一小部分代码。 If the user has voted, clicking the comments button will cause the comments box to open and close immediately. 如果用户已投票,则单击评论按钮将使评论框立即打开和关闭。

$(document).ajaxComplete(function() {
        // Lets user votes on a match
    $('.btn-vote').click(function() {
        ......

        $.getJSON(path + 'includes/ajax/update_votes.php', { id: gameID, vote: btnID }, function(data) {
            ......
        });
    });

        // Connects a match with a disqus thread
    $('.btn-comment').click(function() {
        var parent = $(this).parents('.main-table-drop'), comments = parent.next(".main-table-comment");

        if (comments.is(':hidden')) {
            comments.fadeIn();
        } else {
            comments.fadeOut();
        }
    });
});

Solved the problem by checking the DOM loading ajax request URL 通过检查DOM加载ajax请求URL解决了该问题

$(document).ajaxComplete(event,xhr,settings) {
var url = settings.url, checkAjax = 'list_matches';

if (url.indexOf(checkAjax) >= 0) { ... }

}

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

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