How to use JS to send AJAX requests recursively

function onNetChange() {
        var nt = plus.networkinfo.getCurrentType();                //
            if (nt == "2" || nt == "3" || nt == "5" || nt == "6") {
                mui.toast("");
                var tx = db.transaction("users", READ_WRITE);      //
                var store = tx.objectStore("users");               //
                var index = store.index("Ifnet");                  //
                var req = index.openCursor(IDBKeyRange.only("1")); //
                req.onsuccess = function(evt) {                    //
                    var res = evt.target.result;
                    if (res) {
                        var net = [res.value]
                        for (j = 0; j < net.length; jPP) {
                            mui.ajax("http://XXXXX", {
                                data: {
                                    tugwork_s_id: net[j].TUGWORK_S_ID,
                                    IsMooring: net[j].If_mooring,
                                    IsPickup: net[j].ISPICKUP,
                                    godate: net[j].Godate,
                                    startdate: net[j].Startdate,
                                    enddate: net[j].Enddate,
                                    backdate: net[j].Backdate
                                },
                                dataType: "json",
                                type: "post", 
                                timeout: 5000, 
                                success: function(res) {
                                    console.log(JSON.stringify(res));
                                },
                                error: function(xhr, type, errorThrown) {
                                    console.log("Update_local");
                                }
                            });
                        }
                        res.continue(); //
                    }
                };
                req.onerror = function(evt) {                      //
                console.error("UpData error:", evt.target.errorCode || evt.target.error);
                };
                                                
            } 
            else {
                mui.toast("");
            }                
      }                
            
            
            
            
This is what the

code looks like, and it"s very confusing for me to write and send AJAX. Although the data was saved successfully. I want to write a recursive method but I don"t know how to write it. Ask some great god to help me see how I can make this for loop recursive. For example, I found three pieces of data that meet the criteria locally. Then I send three AJAX requests. Failure is not considered for the time being. By default, it is all successful ideally.

Jun.13,2022

Recursive implementation feels like you still need to use an accumulative variable as a marker to terminate sending the request, which is similar to the reason for the loop you write

if (res) {
  var j = 0
  function request (){
    mui.ajax('http://XXXXX', {
     data: {
       tugwork_s_id: net[j].TUGWORK_S_ID,
       IsMooring: net[j].If_mooring,
       IsPickup: net[j].ISPICKUP,
       godate: net[j].Godate,
       startdate: net[j].Startdate,
       enddate: net[j].Enddate,
       backdate: net[j].Backdate
     },
       dataType: 'json',
       type: 'post', 
       timeout: 5000, 
       success: function(res) {
         console.log(JSON.stringify(res));
       },
       error: function(xhr, type, errorThrown) {
         console.log('Update_local');
       }
    });
    jPP
    if (j < net.length) {
      request ()
    }  
  }
  // 
  request ()

}

solved the difficulty of writing my own
, which is more complicated than I thought.

function onNetChange() {
            var nt = plus.networkinfo.getCurrentType();
            if (nt == "2" || nt == "3" || nt == "5" || nt == "6") {
                mui.toast("");
                plus.device.vibrate();
                s = plus.audio.createPlayer("../audio/NetYes.mp3");
                var num = s.getDuration(); //number 
                setTimeout(function() { // 
                    var num = s.getDuration();
                    // alert(num)
                }, 100)

                s.play(function() { // 
                    // alert("Audio play success!");
                }, function(e) { // 
                    // alert("Audio play error: " + e.message);
                });
                var tx = db.transaction("users", READ_WRITE);
                var store = tx.objectStore("users");
                var index = store.index("Ifnet");
                var req = index.openCursor(IDBKeyRange.only("1"));
                var odiv = document.getElementById("tijiao");
                if (odiv.style.display = "none") {
                    odiv.style.display = "block";
                } 
                req.onsuccess = function(evt) {
                    var res = evt.target.result;
                    if (res) {
                        var net = [res.value]
                        var j = 0;
                        function submit(){
                            if(j>=net.length){ 
                               return;
                            }
                            mui.ajax('http://', {
                                data: {
                                    tugwork_s_id: net[j].TUGWORK_S_ID,
                                    IsMooring: net[j].If_mooring,
                                    IsPickup: net[j].ISPICKUP,
                                    godate: net[j].Godate,
                                    startdate: net[j].Startdate,
                                    enddate: net[j].Enddate,
                                    backdate: net[j].Backdate
                                },
                                dataType: 'json', //json
                                type: 'post', //HTTP
                                timeout: 5000, //10;
                                success: function(req) {
                                    console.log(JSON.stringify(req));
                                    jPP;
                                    submit();
                                },
                                error: function(xhr, type, errorThrown) {
                                    console.log('Update_local');
                                }
                            });                                
                        }
                        submit();
                        res.continue();
                    }
                };
                req.onerror = function(evt) {
                    console.error("UpData error:", evt.target.errorCode || evt.target.error);
                };
                setTimeout(function(){odiv.style.display = "none";}, 3000);                                        
            } else {
                mui.toast("");
                plus.device.vibrate();
                s = plus.audio.createPlayer("../audio/NetNo.mp3");
                var num = s.getDuration(); //number 
                setTimeout(function() { // 
                    var num = s.getDuration();
                    // alert(num)
                }, 100)

                s.play(function() { // 
                    // alert("Audio play success!");
                }, function(e) { // 
                    // alert("Audio play error: " + e.message);
                });
            }
        }
Menu