samedi 25 juillet 2020

Alternitive for Ajax success?

I am currently working with multipule ajax requests and I ran into a problem. Currently, this is what I have:

var x = true;
var RanThroughEverything = false; 
while(x){
  if(ButtonWasClickedByUser){
     ajax{
        //send ajax request and get response;
        success: function(res){ 
             if(res == "What I want it to equal"){
                 x = true;
             }
             else{
                 x = false;
             }
        }
     }
  }
  else{
    x = true;
  }
  if(SecondButtonWasClickedByUser){
     ajax{
        //send second ajax request and get response;
        success: function(res){ 
             if(res == "What I want this one to equal"){
                 x = true;
             }
             else{
                 x = false;
             }
        }
     }
  }
  else{
    x = true;
  }

  x = false;(To break out of function)
  RanThroughEverything = true; 
}
if(x == false && RanThroughEverything){
   alert("One");
}
else if(x == false && RanThroughEverything == false){
   alert("Two")
}

The problem I am having is that no matter what I set the res (or result) of the success function to equal to, it always alerts One. I figured out this is because to get a response from ajax takes time, and by that time, the while loop would have been broken by the x = false nearing the end of the while loop. Is there any way that the ajax responses can register before one iteration of the while loop is complete?

Aucun commentaire:

Enregistrer un commentaire