Jump to content
funbinod

running ajax only once inside a for loop

Recommended Posts

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....

Share this post


Link to post
Share on other sites

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;

 

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...