donglian3055 2018-12-19 17:45
浏览 55

Ajax迭代 - 每3秒重复一次AJAX调用

AJAX call instead of .click load - I would like the ajax to run every 3 seconds till the end of Total_Records. Here is my AJAX code which runs .click of a button.

I am passing two variables 1. id number
2. Total_Records

I would like the ajax to iterate a number of times = Total_Records.

Any suggestions are highly appreciated.

$(document).ready(function() {
    $(document).on('click', '.show_more', function() {
        var ID = $(this).attr('id');
        var lim = $(this).attr('Total_Records');
        $.ajax({
            type: 'POST',
            url: 'moreajax.php',
            data: {
                'id': ID,
                'lim': lim
            },
            success: function(html) {
                setTimeout(function() {
                    $('#show_more_main' + ID).remove();
                    $('.postList').append(html);
                }, delay);
            },
        });
    });
});

Method 2) Please kindly read and recommend any better way or script improvement with examples please. this will benefit everyone.

Note: I believe this method would be a great enhancement and re-usable to lots of people who are looking for interval runs.

The only issue is the cancel button .click is not stopping the ajax call.

 $('#cancel').click(function() {
        cancel = true;
    });

    $(function() {

        //If cancel variable is set to true stop new calls
        if (cancel == true) return;

        var RecordsInterval = 10 * 1000;
        var fetchRecords = function() {
            console.log('Sending DATA AJAX request...');
            $.ajax({
                type: "POST",
                url: "fetchRecords.php",
                data: {
                    'id': ID,
                    'lim': lim
                }
            }).done(function(html) {
                $('#show_more_main' + ID).remove();
                $('#postList').append(html);
                alert("You got it champ")
                console.log('success');
                console.log('Waiting ' + (RecordsInterval / 1000) + ' seconds');
                setTimeout(fetchRecords, RecordsInterval);

            }).fail(function() {
                console.log('error');
                alert("error Champ");
                return;
            });
        }

        // Fetch Records immediately, then every 10 seconds AFTER previous request finishes
        fetchRecords();
    });
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Vue3 大型图片数据拖动排序
    • ¥15 划分vlan后不通了
    • ¥15 GDI处理通道视频时总是带有白色锯齿
    • ¥20 用雷电模拟器安装百达屋apk一直闪退
    • ¥15 算能科技20240506咨询(拒绝大模型回答)
    • ¥15 自适应 AR 模型 参数估计Matlab程序
    • ¥100 角动量包络面如何用MATLAB绘制
    • ¥15 merge函数占用内存过大
    • ¥15 使用EMD去噪处理RML2016数据集时候的原理
    • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大