2012-01-11 16 views
0

私の写真のインポートスクリプトでは、Ajaxリクエストのあるwhileループの後にプログレスバー(Jquery UI)を更新しようとしています。JQueryUIプログレスバーの値を動的に変更する方法は?

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; 
    }); 


    }); 
+0

完全な応答時間はありません。 /したがって、すべての簡潔さの中でいくつかの観察があります:ajax_done = true;およびnb_photos_imported ++;を成功コールバックに設定していますが、ajaxリクエストを定義した直後にチェックしています。最後のチェックを実行する前にほとんどの(すべての)リクエストが終了しないため、結果は正確ではありません。 jQueryの遅延オブジェクトAPI(http://api.jquery.com/category/deferred-object/)を見ることをお勧めします。たぶんそれはあなたにこれをより良く解決する方法のアイデアを与えることができます。あなたが変更を行っている場合は投稿し、後で回答を書き込もうとします。 – polarblau

答えて

0

は私の新しいコードです。コンソールログにはパーセントの進捗状況が表示され、写真はインポートされますが、最後に表示されます。ループのどこかで私のデバッグツールをブレークポイントにすると、ディスプレイは元気で、すべてが正常です。

$(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; 
    }); 
}); 
+0

「最後に表示する」とはどういう意味ですか? (Btw .:新しいものを試してみると元の質問を編集/拡張することができます。現在のバージョンはまだ動作しないので、実際には "回答"とはみなされず、他の回答を得ることができません) – polarblau

+0

I送信ボタンをクリックしてください。画面は変更されません。コンソールログに '成功'メッセージが表示されます。最後に、画面の変更と進行状況バー(完了)が表示されます。 – charade

関連する問題