Results 1 to 10 of 10

Thread: [RESOLVED] how do you loop this

  1. #1

    Thread Starter
    Member
    Join Date
    Oct 2021
    Posts
    38

    Resolved [RESOLVED] how do you loop this

    How do you loop this code to make sure that it is on its ready state and status at 200 and has valid response?

    I did try using While loop but there is no response still or so I think.

    Code:
                          function loadXMLDoc() 
    			{
    			  var xhttp = new XMLHttpRequest();
    			  xhttp.onreadystatechange = function() 
    			  {
    				if (xhttp.readyState == 4 && xhttp.status == 200) 
    				{
    					document.getElementById("p1").innerHTML = xhttp.responseText;					
    				}
    			  };
    			  xhttp.open("POST", "manage.php", true);
    			  xhttp.send();
    			}
    Last edited by UserBee; Nov 5th, 2021 at 01:01 AM.

  2. #2
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,147

    Re: how do you loop this

    When you say "loop this" what do you mean? Are you saying that you want to repeatedly hit the endpoint until you get a 200 response?
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  3. #3

    Thread Starter
    Member
    Join Date
    Oct 2021
    Posts
    38

    Re: how do you loop this

    Quote Originally Posted by dday9 View Post
    When you say "loop this" what do you mean? Are you saying that you want to repeatedly hit the endpoint until you get a 200 response?
    yes, a 200 response, ready state=4 and the response is not null because the server may not respond in time or accordingly

  4. #4
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,147

    Re: how do you loop this

    Call the loadXMLDoc method recursively when your conditions aren't met:
    Code:
    if (xhttp.readyState !== 4 || xhttp.status !== 200 || !xhttp.responseText) {
        loadXMLDoc();
        return;
    }
    document.getElementById("p1").innerHTML = xhttp.responseText;
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  5. #5

    Thread Starter
    Member
    Join Date
    Oct 2021
    Posts
    38

    Re: how do you loop this

    isnt If statement only checked and executed once with that way? how do the return statement affects the loop that way. so that means the return makes the loop back to the if statement?

    on the top of my head I am stuck into thinking on loop statements like while and do.

  6. #6
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,147

    Re: how do you loop this

    Once in that method, but when the method is recursively called it will take place again.
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  7. #7

    Thread Starter
    Member
    Join Date
    Oct 2021
    Posts
    38

    Re: how do you loop this

    how does it recurse? by the return statement? does that mean it will go back to the if statement? sorry for asking because i have not tried that way before. i use the return word for functions only.

  8. #8
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,147

    Re: how do you loop this

    By calling the method just before the return statement.
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

  9. #9

    Thread Starter
    Member
    Join Date
    Oct 2021
    Posts
    38

    Re: how do you loop this

    Quote Originally Posted by dday9 View Post
    By calling the method just before the return statement.
    yeah, so the loadXMLDoc method is recurse by the return keyword but what Im curious is that if the use of that return keyword read back the if statement that way? i just would like to clarify myself on that.

    oh wait, so it will really start from the beginning of the method loadxmldoc, not only from the if statement. i see.

    thanks dday for your patience and assistance. i learned.
    Last edited by UserBee; Nov 5th, 2021 at 12:48 PM.

  10. #10
    Super Moderator dday9's Avatar
    Join Date
    Mar 2011
    Location
    South Louisiana
    Posts
    11,147

    Re: [RESOLVED] how do you loop this

    Correct, when you call a method, regardless of if it is recursive, it will always start at the top of the method definition. In this case, this would be the sequence of events:
    1. Create a new XMLHttpRequest
    2. Listen to the onreadystatechange event of the XMLHttpRequest
    3. Send a POST request
    4. Check if the ready states do not meet your business logic
      1. If not, then repeat steps 1 - 4 and exit the function
      2. If so, then set the innerHTML of the element with the id p1 with the response text
    "Code is like humor. When you have to explain it, itís bad." - Cory House
    VbLessons | Code Tags | Sword of Fury - Jameram

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width