Jquery wait for Ajax call to complete -


if have jquery script move files ftp server 1 ftp server 2. work, want move files 1 one , not @ once. without freezing browser "async=false"..

 <script>     var files = new array();     files[1]="testfile1.txt";     files[2]="testfile2.txt";     files[3]="testfile3.txt";       $('#button').click(function() {       $.each( files, function( key, value ) {         jquery.ajax({         url: '/move.php',         method: 'get',         data: {         file: value         },         success: function(data) {           $('#'+ key).html(data);         }       })     });   });   </script> 

create function next item in files array, perform ajax call, , calls in success callback of ajax call.

var files = new array(), key = 1; // arrays indexed starting @ 0, left below files[1]="testfile1.txt"; files[2]="testfile2.txt"; files[3]="testfile3.txt";   function movefile() {     if(key < files.length) { // there's next element         var value = files[key];         $.ajax({             url: '/move.php',             method: 'get',             data: {                 file: value             },             success: function(data) {                 $('#' + key++).html(data);                 movefile();             }         });     } }  $('#button').click(movefile); 

i considered using files.pop(); next element, since wasn't sure whether wanted array remain intact after had run decided track index access them instead.


Comments

Popular posts from this blog

node.js - Bad Request - node js ajax post -

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -