AJAX继续加载 - 如何绑定或阻止这种情况?

时间:2017-08-29 20:33:42

标签: javascript jquery ajax bind

我现在已经挣扎了几个星期了,我已经尝试了很多方法。

这是一个ajax搜索功能。如果我启动搜索,它会继续加载几次。我搜索的越多,它加载的次数就越多。

这是我的搜索功能:

$("#q").on('submit', function (e) {


        e.preventDefault(); // avoid to execute the actual submit of the form.

        $("#q").off('submit');

        console.log('#################');
        console.log('search Initiated');
        console.log('#################');

        var frm = $('#q');
        var url = frm.attr('action');

        if (reloaddiv === undefined) {
            var reloaddiv = "reload";
        }
        if (reloadurl === undefined) {
            var reloadurl = frm.attr('action');
        }

        $.ajax({
            type: frm.attr('method'),
            url: frm.attr('action'),
            data: frm.serialize(),
            beforeSend: function () {
                console.log('Search: Preparing');
                prepareBeforeAjax(reloaddiv, reloadurl);
            },
            success: function (data) {
                $('.records').html(data);
                console.log('Search: Success');
            },
            complete: function () {
                console.log('Search: Complete');
                hideModalIfVisible();
            },
            always: function () {
                $("#q").on('submit', e);
            }
        })

    });

这是我的控制台日志

#################
subscriptions:1037 search Initiated
subscriptions:1038 #################
subscriptions:1055 Search: Preparing
subscriptions:941 prepareBeforeAjax
subscriptions:1036 #################
subscriptions:1037 search Initiated
subscriptions:1038 #################
subscriptions:1055 Search: Preparing
subscriptions:941 prepareBeforeAjax
subscriptions:1036 #################
subscriptions:1037 search Initiated
subscriptions:1038 #################
subscriptions:1055 Search: Preparing
subscriptions:941 prepareBeforeAjax
subscriptions:1036 #################
subscriptions:1037 search Initiated
subscriptions:1038 #################
subscriptions:1055 Search: Preparing
subscriptions:941 prepareBeforeAjax
subscriptions:1060 Search: Success
subscriptions:975 ------------
subscriptions:976 initReady Initiated
subscriptions:977 ------------
subscriptions:1063 Search: Complete
subscriptions:1060 Search: Success
subscriptions:975 ------------
subscriptions:976 initReady Initiated
subscriptions:977 ------------
subscriptions:1063 Search: Complete
subscriptions:1060 Search: Success
subscriptions:975 ------------
subscriptions:976 initReady Initiated
subscriptions:977 ------------
subscriptions:1063 Search: Complete
subscriptions:1060 Search: Success
subscriptions:975 ------------
subscriptions:976 initReady Initiated
subscriptions:977 ------------
subscriptions:1063 Search: Complete

initReady - 这基本上只是我在每个ajax帖子之后调用的另一个JavaScript函数 - 我这样做是因为在使用模态时,我不必加载所有。

1 个答案:

答案 0 :(得分:-1)

由于以下原因,该函数看起来一直在调用自身:

always: function () {
    $("#q").on('submit', e);
}