javascript - depending loops with ajax request inside -


hi im using jquery run several ajax request in loops built html table.

how can make them run in specific order?

i tryed $.when , in case seem work using async: false,

i got 2 main functions

1) building ajax request url inside loop

for (var = 0; < split.length; i++) {        zeile = split[i];      request = $.ajax({         url: "url.php?q=" + zeile, 

and drawing basic table

success: function(data) {             $.each(data.data, function(key, val) {                 p = p + "<td>...</td>"                      p = p + "<td>...</td>"                                 p = p + "</tr>";                 $("#htmlergebnis").append(p);     

after different request finished , complete table drawn want run second function, adds table cells data source. therefor uses value out of each existing row (drawn first loops) search value request

 $(".ergebniszeile" ).each(function(key) {       ...      var str = "url2.php?q=" + zeile;         $.ajax({             url: "" + str,             type: 'get',             datatype: 'json',             success: function(data) {                   $("#"+rowid+"").children(".value2").append(data.value2); 

i tested .ajaxcomplete , .ajaxstop seem work ajax request not specific group of request inside loop.

i think problem loops built ajax urls. not ajax requests depending on each other loops ajax request inside.

the results + corresponding request built in order

 <tr><th>loop 1</th><th>loop 1</th><th>loop 1</th><th>loop 2</th><th>loop 2</th><tr>  <tr><td>request 1</td><td>request 1</td><td>request 1</td><td>request 4</td><td>request 4</td></tr>  <tr><td>request 1</td><td>request 1</td><td>request 1</td><td>request 5</td><td>request 5</td></tr>  <tr><td>request 1</td><td>request 1</td><td>request 1</td><td>request 6</td><td>request 6</td></tr>  <tr><td>request 2</td><td>request 2</td><td>request 2</td><td>request 7</td><td>request 7</td></tr>  <tr><td>request 2</td><td>request 2</td><td>request 2</td><td>request 8</td><td>request 8</td></tr>  <tr><td>request 3</td><td>request 3</td><td>request 3</td><td>request 9</td><td>request 9</td></tr>  <tr><td>request 3</td><td>request 3</td><td>request 3</td><td>request 10</td><td>request 10</td></tr> 

i not focus on making requests synchronous (on specific order), slow down -unless returned data very big-, have parallel requests.

what decouple fetching of data (requests) -which can done asynchronously- , actual rendering of table.

so instance launch first request populate main bit of table, , launch other requests ready so. on success of these calls, store result in array (together key of request), , when requests finished go through stored results in order (because key of request stored) , generate table then.

by way, instead of concatenating strings

            p = p + "<td>...</td>"                  p = p + "<td>...</td>"                             p = p + "</tr>"; 

use array + join:

            var sb = [];             sb.push("<td>...</td>");             sb.push("<td>...</td>");             sb.push("</tr>");              p += sb.join(''); 

Comments

Popular posts from this blog

PHPMotion implementation - URL based videos (Hosted on separate location) -

javascript - Using Windows Media Player as video fallback for video tag -

c# - Unity IoC Lifetime per HttpRequest for UserStore -