ajaxComplete与getJSON导致循环

时间:2016-07-07 12:25:12

标签: javascript jquery json ajax getjson

在动态内容加载到DOM后,我使用ajaxComplete来运行一些函数。我在ajaxComplete中有两个独立的函数,它们使用getJSON。

  • 一旦正常运行任何功能
  • 第二次运行它们会导致循环,因为它们使用的是getJSON。

我该如何解决这个问题?

我附上了一小部分代码。如果用户已投票,则单击评论按钮将使评论框立即打开和关闭。

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

1 个答案:

答案 0 :(得分:0)

通过检查DOM加载ajax请求URL来解决问题

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

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

}