简体   繁体   English

如何在ajax请求期间显示进度条(jquery / php)

[英]How to display a progress bar during an ajax request (jquery/php)

I have an ajax request, whereby I am installing a magento shop automatically, and when the process is done, it would redirect the user to the newly created shop. 我有一个ajax请求,据此我将自动安装一个magento商店,当该过程完成后,它将把用户重定向到新创建的商店。 Here are my codes: 这是我的代码:

   function postSuccessFormData() {
    var targetUrl = '/index.php/install/wizard/successPost';

    jQuery('.form-button').addClass('loading');

   setInterval(installStatus(),4000);
    jQuery.ajax({
        url: targetUrl,
        global: false,
        type: 'POST',
        data: ({
            finish: 1,
            password_key: jQuery('#password_key').val()
        }),
        async: true,
        dataType: 'json',
        error: function() {
            alert("An error has occurred. Please try again.");
        },
        success: function(data) {
            window.location.href = '/';
        }

    });

function installStatus() {
    var installerUpdatesUrl = '/index.php/install/wizard/installerStatus';

    //showProgressBar();
    jQuery.ajax({
        url: installerUpdatesUrl,
       // global: false,
        type: 'GET',
        async: true,
        dataType: 'json',
        error: function (data) {
          //  alert(data.result);
        },
        success: function (data) {

        handle data.result
        var dataKeys = Object.keys(data);
        var lastElementKey = dataKeys[dataKeys.length - 1];
        var lastMessage = data[lastElementKey]['message'];

       if(data[lastElementKey]['progress'] == '') {
            updateProgressBar(data[dataKeys[dataKeys.length - 2]]['progress'],100);
        }

            setting message
            jQuery("#message").html(lastMessage);

            if (data[lastElementKey]['state'] == 'Failure') {
                var stepStr = lastElementKey.split('_');
                var stepString = stepStr[0].toUpperCase() + ' ' + stepStr[1] + ':';

                alert(stepString + "\n" + data[lastElementKey]['message']);
                //hideProgressBar();
                jQuery('.form-button').removeClass('loading');
                return false;
            } else if (data[lastElementKey]['state'] == 'Finish') {
                alert(data[lastElementKey]['message']);
                //hideProgressBar();
                jQuery('.form-button').removeClass('loading');
                //window.location.href = '/';
            } else {
               // installStatus();
            }
        },
        complete: function () {
            installStatus();
            jQuery('.form-button').removeClass('loading');

        }
    });
}

The way this is done: 完成的方式:

After every 4 seconds the function installStatus is run, which will output the current progress in JSON format. 每隔4秒钟运行一次功能installStatus,它将以JSON格式输出当前进度。 My problem is, this function needs to be executed simultaneously with the function post(). 我的问题是,此函数需要与函数post()同时执行。

This is not happening, the installStatus is only run after the first function has been completed. 这没有发生,仅在第一个功能完成后才运行installStatus。

What is wrong? 怎么了?

You are executing installStatus when you define it. 定义时,您正在执行installStatus So this: 所以这:

setInterval(installStatus(),4000);

needs to be 需要是

setInterval(installStatus, 4000);

The new XMLHttpRequest has a nice progress event you can listen to show the user the upload progress. 新的XMLHttpRequest有一个不错的progress事件,您可以收听以向用户显示上传进度。

Here's the spec with a nice demo: https://developer.mozilla.org/en/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Monitoring_progress 这是带有一个不错的演示的规范: https : //developer.mozilla.org/en/docs/Web/API/XMLHttpRequest/Using_XMLHttpRequest#Monitoring_progress

Initially you should call installStatus() only once and then inside the method inside ajax success you should update the procent in the progress bar and call it recursively the same method. 最初,您应该只调用一次installStatus(),然后在ajax成功内的方法内部,应在进度栏中更新procent,然后递归调用相同的方法。 On the server side you can save the current procent in a cookie and with every recursive call you can update the cookie and return the procent. 在服务器端,您可以将当前procent保存在cookie中,并且通过每次递归调用,您都可以更新cookie并返回procent。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM