【问题标题】:How to change the value of a JQueryUI progressbar dynamically?如何动态更改 JQueryUI 进度条的值?
【发布时间】:2012-01-11 15:39:13
【问题描述】:

在我的导入照片脚本中,我尝试在带有 Ajax 请求的 while 循环之后更新进度条(Jquery UI)。

JQueryUI 进度条,它应该在 ajax 请求启动之前显示,之后执行。所以我在我的while循环执行结束时有用户反馈......(控制台向我显示这些应用程序运行良好,顺序正确)

想过用live()或者bind(),但是不知道怎么测试……

这是我的 jquery 代码:

    $(function() {

    var percent_progressbar=0;

    $('#myform').submit(function() {

        $('#myform').hide(); 
        $("#text_result").html("Import in progress : <span class='progression'>0/0</span>").show(10, function() {

        $("#myprogressbar").progressbar({value:percent_progressbar});           
        $("#myprogressbar").show(10, function() {

            if (jQuery.trim($("#id_src").val()).length!=0) {
            // Total number of photos to import
            $.ajax({
                type : 'POST',
                async : false,
                url : '/nbphotos.php',
                data : 'chem=mydir',
                success : function(nbphotos){
                if (nbphotos>0){
                    $(".progression").html("0"+" / "+nbphotos);

                    var finish=false;
                    var nb_photos_imported=0;
                    var ajax_done = false;
                    var resultat_ajax="";
                    // Variables envoyées en Ajax
                    var datas = desvariables;
                    while (nb_photos_imported < nbphotos) {

                        ajax_done = false;

                        $.ajax({
                        type : 'GET',
                        async : false,
                        url : '/traitement.php',
                        data : datas,
                        success : function(resultat){
                            resultat_ajax=resultat;
                            ajax_done=true;
                            nb_photos_imported++;
                        }
                        });

                        if (ajax_done==true){
                        if (resultat_ajax=="ok") {
                            percent_progressbar = Math.floor(nb_photos_imported*100/nbphotos);
                            $("#myprogressbar").progressbar("option", "value", percent_progressbar);
                            console.info("Succès ajax (resultat : OK) : "+percent_progressbar);
                            $(".progression").html(nb_photos_imported+" / "+nbphotos);
                        } else {
                            finish=true;
                            $("#text_result").html(ajax_done);
                            return false;
                        }                   
                        } else {
                        // ajax error => exit the while
                        console.info("Ajax execution error "+nb_photos_imported);
                        return false;
                        }                   
                    } // While

                    if (finish==true) {
                        $("#myprogressbar").hide('slow');
                        $('#text_result').html("Import of "+nb_photos_imported+" photos done.");
                    }

                } else{
                    $("#text_result").html("No photo to import.");              
                    $("#myprogressbar").hide(10);
                    $('#myform').show("slow"); 
                }
                }
            });
            }
        });
        }); // callback #text_result.show()

        return false;
    });


    });

【问题讨论】:

  • 没有时间获得完整的答案,抱歉。 / 因此,简而言之,有几点观察:您在成功回调中设置ajax_done = true;nb_photos_imported++;,但在定义ajax 请求后立即对其进行检查。结果将不准确,因为在您运行最后一次检查之前,大多数(所有)请求甚至都不会完成。我建议查看 jQuery 的延迟对象 API:api.jquery.com/category/deferred-object。也许这可以让您了解如何更好地解决这个问题。如果您要进行更改,请发布,我稍后会尝试写一个答案。

标签: jquery-ui jquery progress-bar


【解决方案1】:

这是我的新代码。控制台日志显示百分比的进度,照片已导入,但最后我仍然有显示。如果我在循环的某处用我的调试工具设置断点,显示会刷新并且一切正常...

$(function () {

    $("#myprogressbar").show();

    var RetourNbPhotos = "";
    var RetourImported = "";
    var fini = false;
    var percent_progressbar = 0;
    var nb_photos_imported = 0;
    var datas = desvariables; // datas sent with ajax
    // Number of photos to import
    function NbPhotoAjax() {
        return $.ajax({
            type: 'POST',
            async: false,
            url: '/boutique/modules/importimageswithiptc/ajax-nbphotos.php',
            data: 'chem=$cheminimport',
            success: function (nbphotos) {
                RetourNbPhotos = nbphotos;
            }
        });
    }

    // Photo Ajax Import
    function ImportPhotoAjax() {

        $("#text_result").html("Import in progress : <span class='progression'>0/0</span>").show();

        // RetourImported = "";
        return $.ajax({
            type: 'GET',
            async: false,
            url: '/boutique/modules/importimageswithiptc/ajax-traitement.php',
            data: datas,
            success: function (resultat) {
                RetourImported = resultat;
            }
        });
    }

    $("#myprogressbar").progressbar({
        value: percent_progressbar
    });

    $('#generator').submit(function () {
        $('#generator').hide();
        if (jQuery.trim($("#id_product_src").val()).length != 0) {

            $.when(NbPhotoAjax()).then(function () {
                if (RetourNbPhotos > 0) {
                    while (nb_photos_imported < RetourNbPhotos) {
                        $.when(ImportPhotoAjax()).then(function () {
                            nb_photos_imported++;
                            if (RetourImported == "ok") {
                                percent_progressbar = Math.floor(nb_photos_imported * 100 / RetourNbPhotos);
                                $("#myprogressbar").progressbar("option", "value", percent_progressbar);
                                console.info("Success : " + percent_progressbar + "%");
                                $(".progression").html(nb_photos_imported + " / " + RetourNbPhotos);
                            } else {
                                fini = true;
                                $("#text_result").html("ajax_reussi");
                                return false;
                            }
                        }).fail(function () {
                            //  ajax error => exit the while
                            console.info("Ajax execution error " + nb_photos_imported);
                            return false;
                        });
                    } // While
                    if (fini == true) {
                        $("#myprogressbar").hide('slow');
                        $('#text_result').html("Import of " + nb_photos_imported + " photos done.");
                    }

                } else {
                    $("#text_result").html("No photo to import.");

                    $("#myprogressbar").hide(10);
                    $('#generator').show("slow");
                }
            });
        }
        return false;
    });
});

【讨论】:

  • “最后显示”是什么意思? (顺便说一句:如果您尝试新的东西,您可以编辑/扩展您的原始问题。由于您当前的版本仍然无法正常工作,这并不真正符合“答案”的条件,并且可能会让您无法获得其他答案。)跨度>
  • 我点击提交按钮。画面不变。 ajax 查询运行良好(控制台日志显示“成功”消息)然后,最后,我的屏幕更改和我的进度条(完成)显示。
猜你喜欢
  • 2020-05-16
  • 2023-03-14
  • 2016-05-25
  • 2012-09-04
  • 1970-01-01
  • 1970-01-01
  • 2012-04-21
  • 2023-04-09
  • 2012-05-26
相关资源
最近更新 更多