【问题标题】:Multiple Ajax Calls while one Ajax call is still running在一个 Ajax 调用仍在运行时进行多个 Ajax 调用
【发布时间】:2012-12-17 14:04:53
【问题描述】:

我遇到了一些进度条更新问题。我喜欢做的是,触发一个需要一些时间才能完成的 ajax 调用,在此调用期间,我想触发由间隔设置的 ajax 调用,以更新我的进度条。

由于我找不到解决方案,只找到了浏览器的限制,这将与此处匹配。始终最多有 2 个呼叫处于活动状态。

不过,我的第二个呼叫在 Google Chrome 中仍处于待处理状态,直到我的第一个(主要)呼叫完成。

编辑:完整的 jquery 脚本

// update cars cache
$('#cars_cache_update_run').bind('click', function(){
    // remove button
    $(this).remove();

    // hide import widgets
    $('#products_import_widget').css('display', 'none');
    $('#vehicles_import_widget').css('display', 'none');
    $('#orders_import_widget').css('display', 'none');
    $('#test_data_widget').css('display', 'none');

    // show blind
    $('#cars_cache_update_info').css('display', 'none');
    $('#cars_cache_update_blind').css('display', 'inline');

    var carsUpdateInterval = setInterval(function() {
        getImportState('http://localhost/index.php/import/import_state/cache_update', 'cars_import_progressbar');
    }, 1000);
    // ajax request
    $.ajax({
        url: "http://localhost/index.php/import/cars_cache",
        async : true,
        success: function(data){
            $('#cars_cache_update_blind').css('display', 'none');
            $('#cars_cache_update_success').css('display', 'inline');
            clearInterval(carsUpdateInterval);

        },
        error: function(thrownError){
            $('#cars_cache_update_blind').css('display', 'none');
            $('#cars_cache_update_error').css('display', 'inline');
            $('#cars_cache_update_error_msg').html(thrownError);
        }
    });
});

function getImportState(url, id)
{
    $.ajax({
        url: url,
        success: function(data){
            var json = $.parseJSON(data);

            $.each(json, function(i, item) {
                var progressbar_value = json[i]['state'];
                $( "#"+id ).progressbar({
                    value: progressbar_value
                });
            })
        }
    });
}

另一个有趣的事情,如果我通过 $.get 调用间隔请求,我会得到一个奇怪的错误.. 使用 Codeignitor 框架。

GET http://localhost/[object%20Object] 404 (Not Found) jquery.1.7.min.js:4
send jquery.1.7.min.js:4
f.extend.ajax jquery.1.7.min.js:4
f.(anonymous function) jquery.1.7.min.js:4
(anonymous function)

非常感谢您的帮助,已经尝试了几个小时。也许我只是个菜鸟。哈哈。

无根

【问题讨论】:

  • 你是如何在服务器端处理这个问题的,因为在对同一个后端进行多个 ajax 调用时,通常每个会话只请求一个请求等问题。
  • 尝试封装在一个外部函数中,没有成功。
  • adneo:我只是在 Codeigniter 框架中调用一个控制器/方法.. 没想到。您知道如何测试或增加每个会话的请求值吗?

标签: php jquery ajax simultaneous-calls


【解决方案1】:

不要使用间隔调用多个 ajax。试试这样的:

function updateProgress(){
    $.ajax({
        url: "http://localhost/index.php/import/import_state/cache_update",
        success: function(data){
            var json = $.parseJSON(data);
            $.each(json, function(i, item) {
                var progressbar_value = json[i]['state'];
                $( "#cars_import_progressbar" ).progressbar({
                    value: progressbar_value
                });
            });
            updateProgress(); // after success, call the same function again
        }
    });
}

updateProgress(); // start updateProgress

希望对你有帮助:]

【讨论】:

  • 我明白你现在的意思了——我试图将 updateProgressFile 请求放在实际的 updateFile 请求之前。结果以一个 updateProgressFile 请求结束,在 updateFile 调用后它再次挂起挂起..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-29
相关资源
最近更新 更多