funbinod Posted March 10, 2018 Share Posted March 10, 2018 hello all, i am trying to run an ajax request inside a for loop. and when the response is true, i want to run another ajax only once. but the second ajax also runs multiple times. here is the code i tried .... var i; for (i=0; i< feeid.length; i++) { $.ajax({ url: "submitnewjournal.php", method: "POST", data: { m: m, fullvn: fullvn, vn: vn, }, }) .done (function(result) { var success = $(result).filter("#success").text(); $("#msg").text($(result).filter("#msg").text()); if (success == "NO") { $('#pwait').hide('clip', "fast"); $("#msg").css("color", "red").show("bounce", 100); $('#feelist, #studentlist').show('fade', 250); } else { $("#msg").css("color", "green").show("bounce", 100); $("#fullvn").text($(result).filter("#newfullvn").text()); $("#prefix").text($(result).filter("#newprefix").text()); $("#vn").text($(result).filter("#newvn").text()); /***********************************************************************************************/ /************************* this is the part i want to run onle one time*************************/ /***********************************************************************************************/ if (sendnotice == "Y") { $("#msg").text("Invoice saved successfully. Now sending notification!"); var target = "< PARENTS >"; var receipients = "INVOICE"; var clas = $('#clas :selected').val(); var sec = $('#sec').val(); $.ajax({ url: "noticeprocess.php", method: "POST", data: { target: target, targetid: target, receipient: receipients, token : receipients } }) .done(function(result) { $("#msg").hide("fade", 100).empty().text($(result).filter("#msg").text()); var success = $(result).filter("#success").text(); if (success == "YES") { $("#msg").css("color", "green").show("bounce", 100); $("#pwait").hide("clip", "fast"); // $('#mainarea').load('mnthsalary.php'); } else if (success == "NO") { $("#msg").css("color", "red").show("bounce", 100); } }) .fail(function(e) { $("#msg").hide("fade", 100).empty().text("Connection Error!").css("color", "red").show("bounce", 100); }) window.open("print.php?m=newmnthinvoice&invno=" + fullvn, "_new"); /*********************************************************/ } else { $("#msg").text("Invoice saved successfully."); $('#pwait').hide('clip', "fast"); } } }) .fail(function() { $('#pwait').hide('clip', "fast"); $('#msg').text('Connection problem!').css('color', 'red'); $('#feelist, #studentlist').show('fade', 250); }) } please guide me how can i achieve this. thank u in advance.... Link to comment Share on other sites More sharing options...
Ingolme Posted March 10, 2018 Share Posted March 10, 2018 The asynchronous nature of AJAX means that the code inside the done() and fail() methods only begin running long after the loop ended. Given your current code structure, the easiest way to solve it would be to keep track of whether it was sent already in a global variable. The following code will only work if the noticeSent variable is in the global scope. First define the global variable outside of the loop. var i; var noticeSent = false; for (i=0; i< feeid.length; i++) { When an AJAX response is returned, check the noticeSent variable and only send the notice if it hasn't already been sent: if (sendnotice == "Y" && !noticeSent) { noticeSent = true; 1 Link to comment Share on other sites More sharing options...
funbinod Posted March 11, 2018 Author Share Posted March 11, 2018 wow! it worked nicely! thank you for the guidance! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now