【问题标题】:ajaxComplete with getJSON causing loopajaxComplete 与 getJSON 导致循环
【发布时间】:2016-07-07 12:25:11
【问题描述】:

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

  • 运行任何函数一次都可以正常工作
  • 再次运行它们中的任何一个都会导致循环,因为它们正在使用 getJSON。

我该如何解决这个问题?

我附上了一小部分代码。如果用户已经投票,点击 cmets 按钮将导致 cmets 框立即打开和关闭。

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

【问题讨论】:

  • 为了更好地解释场景,请发布一段代码。
  • @BorjaTur 我编辑了上面的帖子

标签: javascript jquery json ajax getjson


【解决方案1】:

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

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

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    • 1970-01-01
    • 2016-04-03
    • 2020-05-30
    • 1970-01-01
    相关资源
    最近更新 更多