Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jul 31st, 2012  |  syntax: None  |  size: 1.92 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Passing loop count into ajax success anon function
  2. $.ajax({
  3.   url: 'api/dates.json',
  4.   dataType: 'json',
  5.   success: function(d1){
  6.     $('#scan-interactive').removeClass('disabled');
  7.     for(var key in d1.periods){
  8.  
  9.       var html = "";
  10.       html += '<tr id="'+d1.periods[key].id+'">';
  11.       html += '<td class="month">'+d1.periods[key].month+'</td>';
  12.       html += '<td class="week">'+d1.periods[key].week+'</td>';
  13.       html += '<td class="days">'+d1.periods[key].days+'</td>';
  14.       html += '<td class="dates">'+d1.periods[key].dates+'</td>';
  15.       html += '<td class="open"></td>';
  16.       html += '<td class="refunded"></td>';
  17.       html += '<td class="closed"></td>';
  18.       html += "</tr>";
  19.       $('#scan-interactive table tbody').append(html);
  20.  
  21.       $.ajax({
  22.         type: 'POST',
  23.         url: 'api/count.json',
  24.         dataType: 'json',
  25.         data:{'created_at_min':d1.periods[key].created_at_min,'created_at_max':d1.periods[key].created_at_max},
  26.         success: function(d2){
  27.           $('#'+d1.periods[key].id+" .open").html(d2.open);
  28.           $('#'+d1.periods[key].id+" .closed").html(d2.closed);
  29.           $('#'+d1.periods[key].id+" .returns").html(d2.returns);                  
  30.         }
  31.       });
  32.  
  33.     }
  34.   }
  35. });
  36.        
  37. (function(key){
  38.  
  39.   $.ajax({
  40.     type: 'POST',
  41.     url: 'api/count.json',
  42.     dataType: 'json',
  43.     data:{'created_at_min':d1.periods[key].created_at_min,'created_at_max':d1.periods[key].created_at_max},
  44.     success: function(d2){
  45.       $('#'+d1.periods[key].id+" .open").html(d2.open);
  46.       $('#'+d1.periods[key].id+" .closed").html(d2.closed);
  47.       $('#'+d1.periods[key].id+" .returns").html(d2.returns);                  
  48.     }
  49.   });
  50.  
  51. })(key);
  52.        
  53. for (var i = 0; i < 5; i++) {
  54.     $.ajax({
  55.         url: "/echo/html",
  56.         method: "post",
  57.         data: {
  58.             html: "foo",
  59.             delay: 1
  60.         },
  61.         context: {key: i},
  62.         success: function(){
  63.             console.log(this.key);
  64.         }
  65.     });
  66. }