2012-02-15 14 views
0

文字列をどのように表示するか(赤、青、橙、黒)を取得しようとしています。何らかの理由で、注文をランダムに追加します。例えば:出力する(青、オレンジ、赤、黒)。どんな助けも素晴らしいだろう。ありがとう。追加注文 - jQuery

var tCookie = "red,blue,orange,black"; 
var Cookies = tCookie.split(','); 

if (Cookies) { 
    for (var i = 1; i <= Cookies.length; i++) { 

     var dataString = "TabId="+Cookies[i]+""; 

     $.ajax({ 
      type: "POST", 
      url: "includes/tab.php", 
      data: dataString, 
      cache: false, 
      success: function(html){ 
       $("#Dynamic_Tab").append(html).children(':last').hide().fadeIn("fast"); 
      } 
     }); 
    } 
} 
+0

HTML応答 –

答えて

0

あなたが要求と応答のリストを持っている、そしてすべてが終わったときに追加始める、順序が常に正しくなるようにできます。

var deferreds = [], 
    results = []; 

for (var i = 1; i <= Cookies.length; i++) { 
    (function(i) { // to freeze i 

     var dataString = "TabId="+Cookies[i]+""; 

     deferreds.push($.ajax({ 
      type: "POST", 
      url: "includes/tab.php", 
      data: dataString, 
      cache: false, 
      success: function(html){ 
       results[i] = html; // insert at the synchronous position 
      } 
     })); 

    })(i); 
} 

$.when.apply($, deferreds).then(function() { 
    $.each(results, function(i, html) { 
     $("#Dynamic_Tab").append(html).children(':last').hide().fadeIn("fast"); 
    }); 
}); 
+0

asyncを表示してください: "false"は例にはないと思われます。私の失敗 – Joe

+0

@ Joe: 'async'の部分を削除したのが見えますが、これはデフォルトであるため' true'です。 – pimvdb

+0

私はこの問題を非同期を使わずに解決しようとしています。なぜなら、私が見たことを表示する前にスクリプト全体を読み込むからです。非同期なしでそれを行う方法はありますか? – Joe

1

あなただけ追加するには、ここ繰延オブジェクトを使用することができますAJAX要求のすべての後のHTMLは、戻ってくる:それぞれの新しい行は順番に、フェードインするように

//create array to store XHR objects that will resolve when the AJAX requests return 
//also create an object to store the AJAX responses 
var jqXHRs = [], 
    responses = {}; 

//iterate through each of the cookie indexes 
$.each(cookies, function (index, value) { 

    //create the dataString and cache the value of this index so it can be used in the success callback for the AJAX request associated with this index 
    var dataString = "TabId=" + value, 
     thisValue = value; 

    //store an empty string in the output variable for the current index, this keeps it's place in-line 
    responses[thisValue] = ''; 

    //do the AJAX request and store it's XHR object in the array with the rest 
    jqXHRs[jqXHRs.length] = $.ajax({ 
     type : "POST", 
     url  : "includes/tab.php", 
     data : dataString, 
     cache : false, 
     success : function (html) { 

      //now that the AJAX request has returned successfully, add the returned HTML to the output variable for this index 
      responses[thisValue] = html; 
     } 
    }); 
}); 

//wait for all of the XHR objects to resolve then add all the HTML to the DOM 
$.when(jqXHRs).then(function() { 

    //all of the AJAX requests have come back and you can now add stuff to the DOM 
    var $element = $("#Dynamic_Tab"); 
    $.each(responses, function (index, value) { 
     $element.append(value).children(':last').hide().delay(index * 250).fadeIn(250); 
    } 
}); 

.delay()です。

+0

Thanks Jasper。私は考えを得ると思う – Joe

関連する問題