列车交会次数为阿贾克斯成功匿名函数数为、函数、列车、阿贾克斯

2023-09-10 18:15:24 作者:Daniella彡搁浅

在以下键,例如等于 d1.periods 每次的长度。是他们除了通过数据后变量,要获得成功匿名函数?

  $。阿贾克斯({
  网址:API / dates.json',
  数据类型:JSON,
  成功:函数(D1){
    $('#扫描互动)removeClass移除(已禁用)。
    为(在d1.periods VAR键){

      VAR HTML =;
      HTML + ='< TR ID =+ d1.periods [关键] .ID +'>';
      HTML + ='< TD类=月>'+ d1.periods [关键] .month +'< / TD>';
      HTML + ='< TD类=周>'+ d1.periods [关键] .week +'< / TD>';
      HTML + ='< TD类=天>'+ d1.periods [关键] .days +'< / TD>';
      HTML + ='< TD类=日期>'+ d1.periods [关键] .dates +'< / TD>';
      HTML + ='< TD类=开放>< / TD>';
      HTML + ='< TD类=退还>< / TD>';
      HTML + ='< TD类=封闭>< / TD>';
      HTML + =< / TR>中;
      $('#扫描交互式表格TBODY)追加(HTML);

      $阿贾克斯({
        键入:POST,
        网址:API / count.json',
        数据类型:JSON,
        data:{'created_at_min':d1.periods[key].created_at_min,'created_at_max':d1.periods[key].created_at_max},
        成功:函数(D2){
          $('#'+ d1.periods [关键] .ID +。开)HTML(d2.open)。
          $('#'+ d1.periods [关键] .ID +.closed)HTML(d2.closed)。
          $('#'+ d1.periods [关键] .ID +.returns)HTML(d2.returns)。
        }
      });

    }
  }
});
 

解决方案

包装在一个闭包:

 (功能(键){

  $阿贾克斯({
    键入:POST,
    网址:API / count.json',
    数据类型:JSON,
    data:{'created_at_min':d1.periods[key].created_at_min,'created_at_max':d1.periods[key].created_at_max},
    成功:函数(D2){
      $('#'+ d1.periods [关键] .ID +。开)HTML(d2.open)。
      $('#'+ d1.periods [关键] .ID +.closed)HTML(d2.closed)。
      $('#'+ d1.periods [关键] .ID +.returns)HTML(d2.returns)。
    }
  });

})(键);
 
javascript 匿名函数的用途到底是啥

In the example below key is equal to the length of d1.periods everytime. Is their a better way besides passing key into data as a post variable, to get key into successes anonymous function?

$.ajax({
  url: 'api/dates.json',
  dataType: 'json',
  success: function(d1){
    $('#scan-interactive').removeClass('disabled');
    for(var key in d1.periods){

      var html = "";
      html += '<tr id="'+d1.periods[key].id+'">';
      html += '<td class="month">'+d1.periods[key].month+'</td>';
      html += '<td class="week">'+d1.periods[key].week+'</td>';
      html += '<td class="days">'+d1.periods[key].days+'</td>';
      html += '<td class="dates">'+d1.periods[key].dates+'</td>';
      html += '<td class="open"></td>';
      html += '<td class="refunded"></td>';
      html += '<td class="closed"></td>';
      html += "</tr>";
      $('#scan-interactive table tbody').append(html);

      $.ajax({
        type: 'POST',
        url: 'api/count.json',
        dataType: 'json',
        data:{'created_at_min':d1.periods[key].created_at_min,'created_at_max':d1.periods[key].created_at_max},
        success: function(d2){
          $('#'+d1.periods[key].id+" .open").html(d2.open);
          $('#'+d1.periods[key].id+" .closed").html(d2.closed);
          $('#'+d1.periods[key].id+" .returns").html(d2.returns);                  
        }
      });

    }
  }
});

解决方案

Wrap it in a closure:

(function(key){

  $.ajax({
    type: 'POST',
    url: 'api/count.json',
    dataType: 'json',
    data:{'created_at_min':d1.periods[key].created_at_min,'created_at_max':d1.periods[key].created_at_max},
    success: function(d2){
      $('#'+d1.periods[key].id+" .open").html(d2.open);
      $('#'+d1.periods[key].id+" .closed").html(d2.closed);
      $('#'+d1.periods[key].id+" .returns").html(d2.returns);                  
    }
  });

})(key);