【问题标题】:Jquery:: Ajax powered progress bar?Jquery:: Ajax 驱动的进度条?
【发布时间】:2011-05-29 01:37:44
【问题描述】:

我有一个页面,它使用 jquery 的 ajax 函数来发送一些消息。

可能有超过 50k 条消息要发送。

这显然需要一些时间。

我要做的是显示正在发送消息的进度条。

后端是PHP。

我该怎么做?


我的解决方案: 通过原始 ajax 调用中的唯一标识符发送。
此标识符与完成百分比一起存储在数据库(或以标识符命名的文件等)中。

这会随着原始脚本的进行而更新。

设置了一个名为progress(ident)的函数

该函数对读取百分比的脚本进行 ajax 调用。
进度条已更新 如果返回的百分比不是100,
该函数设置了一个超时,在 1 秒后调用自身。

【问题讨论】:

  • 不错,好主意。我很高兴你让它工作!

标签: php javascript jquery ajax progress-bar


【解决方案1】:

如果你使用 jQuery,请检查: http://docs.jquery.com/UI/Progressbar

您可以在每次 AJAX 成功时提供条形的值。

否则,如果您不使用 JS 框架,请参阅: http://www.redips.net/javascript/ajax-progress-bar/

我没有办法测试它,但它应该是这样的:

    var current = 0;
    var total = 0;
    var total_emails = <?php $total_emails ;?>; 

    $.ajax({

      ...
      success: function(data) {
        current++; // Add one to the current number of processed emails
        total = (current/total_emails)*100; // Get the percent of the processed emails
        $("#progressbar").progressbar("value", total); // Add the new value to the progress bar
      }
    });

并确保将 jQuery 与 jQueryUI 一起包含在内,然后在页面的某处添加#progressbar 容器。

虽然我可能有一些错误... 您可能需要对总数进行四舍五入,尤其是在您有很多电子邮件的情况下。

【讨论】:

  • 是的,我将使用进度条,我的主要问题是,我如何获得进度?
【解决方案2】:

您可以通过.html() 将动画 gif 加载到结果区域,直到您的 ajax 函数返回结果。只是一个想法。

关于 jquery ui 进度条,您需要在脚本中间歇性地回显一个表示完成百分比的数值作为分配的 javascript 变量。比如……

// text example php script
if (isset($_GET['twentyfive-percent'])) {
    sleep(2); // I used sleep() to simulate processing
    echo '$("#progressbar").progressbar({ value: 25 });';
    }
if (isset($_GET['fifty-percent'])) {
    sleep(2);
    echo '$("#progressbar").progressbar({ value: 50 });';
    }
if (isset($_GET['seventyfive-percent'])) {
    sleep(2);
    echo '$("#progressbar").progressbar({ value: 75 });';
    }
if (isset($_GET['onehundred-percent'])) {
    sleep(2);
    echo '$("#progressbar").progressbar({ value: 100 });';
    }

下面是我用来获取进度条以更新其位置的函数。有点疯狂,我知道。

avail_elem = 0;
function progress_bar() {
    progress_status = $('#progressbar').progressbar('value');
    progress_status_avail = ['twentyfive-percent', 'fifty-percent', 'seventyfive-percent', 'onehundred-percent'];
    if (progress_status != '100') {
        $.ajax({
            url: 'test.php?' + progress_status_avail[avail_elem],
            success: function(msg) {
                eval(msg);
                avail_elem++;
                progress_bar();
                }
            });
        }
    }

如果我不得不猜测,我敢打赌会有更好的方法...但是当我测试它时,这就是它的工作方式。

【讨论】:

  • 我可能要疯了,但不是只有在收到整个响应时才调用成功吗?
  • 没错,这就是为什么我不得不将 php 脚本分解为 4 个不同的函数并进行 4 个 ajax 调用。
【解决方案3】:

Use this answered question

这就是我实现它的方式:

    var progressTrigger;
    var progressElem = $('span#progressCounter');
    var resultsElem = $('span#results');
    var recordCount = 0;

    $.ajax({
        type: "POST",
        url: "Granules.asmx/Search",
        data: "{wtk: '" + wkt + "', insideOnly: '" + properties.insideOnly + "', satellites: '" + satIds + "', startDate: '" + strDateFrom + "', endDate: '" + strDateTo + "'}",
        contentType: "application/json; charset=utf-8",
        dataType: "xml",
        success: function (xml) {
            Map.LoadKML(xml);
        },
        beforeSend: function (thisXHR) {
            progressElem.html(" Waiting for response from server ...");
            ResultsWindow.LoadingStart();

            progressTrigger = setInterval(function () {
                if (thisXHR.readyState > 2) {
                    var totalBytes = thisXHR.getResponseHeader('Content-length');
                    var dlBytes = thisXHR.responseText.length;
                    (totalBytes > 0) ? progressElem.html("Downloading: " + Math.round((dlBytes / totalBytes) * 100) + "%") : "Downloading: " + progressElem.html(Math.round(dlBytes / 1024) + "K");
                }
            }, 200);


        },
        complete: function () {
            clearInterval(progressTrigger);
            progressElem.html("");
            resultsElem.html(recordCount);
            ResultsWindow.LoadingEnd();
        },
        failure: function (msg) {
            var message = new ControlPanel.Message("<p>There was an error on search.</p><p>" + msg + "</p>", ControlPanel.Message.Type.ERROR);
        }
    });

【讨论】:

  • 这似乎是一个很好的方法,因为如果你想要进度条来衡量后台进程的进度,你不想通过多个 ajax 请求来访问服务器。 . 我要测试这个方法,看看我能不能让它发挥作用!
猜你喜欢
  • 1970-01-01
  • 2011-12-21
  • 1970-01-01
  • 1970-01-01
  • 2015-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多