-
Nov 5th, 2021, 12:19 AM
#1
Thread Starter
Member
[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.
-
Nov 5th, 2021, 11:52 AM
#2
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?
-
Nov 5th, 2021, 11:59 AM
#3
Thread Starter
Member
Re: how do you loop this
Originally Posted by dday9
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
-
Nov 5th, 2021, 12:05 PM
#4
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;
-
Nov 5th, 2021, 12:10 PM
#5
Thread Starter
Member
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.
-
Nov 5th, 2021, 12:11 PM
#6
Re: how do you loop this
Once in that method, but when the method is recursively called it will take place again.
-
Nov 5th, 2021, 12:14 PM
#7
Thread Starter
Member
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.
-
Nov 5th, 2021, 12:15 PM
#8
Re: how do you loop this
By calling the method just before the return statement.
-
Nov 5th, 2021, 12:20 PM
#9
Thread Starter
Member
Re: how do you loop this
Originally Posted by dday9
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.
-
Nov 5th, 2021, 12:46 PM
#10
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:
- Create a new XMLHttpRequest
- Listen to the onreadystatechange event of the XMLHttpRequest
- Send a POST request
- Check if the ready states do not meet your business logic
- If not, then repeat steps 1 - 4 and exit the function
- If so, then set the innerHTML of the element with the id p1 with the response text
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|