jQuery .ajax() access to beforeSend local variable in javascript loop -



have jquery ajax call inside javascript cycle.
here code:

    var number_of_ping_for_average = 4; var ping_start_time;     for(i = 0; i++; < number_of_ping_for_average){                  $.ajax({                     type: 'get',                     url: "http://www.exmple.com/pkt.ext",                     timeout: 1000,                     cache: false,                     beforesend: function(){                         ping_start_time[i][new date().gettime()];                     },                     success: function (data) {                         var ping_arrive_time = new date().gettime();                         var ping_val = ping_arrive_time - ping_start_time[i];                     },                     error: function(data){                         //timeout or 500 error                         //@todo fare funzionare tutto                     }                 });             } 

as see... need call variable "i" inside anonymous function... don't work
remember ping_start_time must contain 3 different simultaneous value!

create separate function ajax call , should work, creating separate function ensures value of not change inn scope.

  var number_of_ping_for_average = 4;     var ping_start_time=[];      function callsajax(i){         $.ajax({         type: 'get',         url: "http://www.exmple.com/pkt.ext",         timeout: 1000,         cache: false,         beforesend: function(){           ping_start_time[i][new date().gettime()];          },         success: function (data) {           var ping_arrive_time = new date().gettime();           var ping_val = ping_arrive_time - ping_start_time[i];          },          error: function(data){            //timeout or 500 error           //@todo fare funzionare tutto          }       });     }      for(i = 0;i < number_of_ping_for_average;i++){         callsajax(i)                         } 

Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -