Jump to content

Looping a server connection?


bobleny

Recommended Posts

OK, I created a script that is confusing the hell into me!  >:(

 

fooy.php

<?php
echo "Fooy on you!<br />";
?>

 

foo.php

<html>
<head>
	<script type="text/javascript">
		<!--
			var GetServer
			function Connect()
			{
				try
				{
					// Firefox, Opera 8.0+, Safari
					GetServer = new XMLHttpRequest();
				}
				catch (e)
				{
					// Internet Explorer
					try
					{
						GetServer = new ActiveXObject("Msxml2.XMLHTTP");
					}
					catch (e)
					{
						try
						{
							GetServer = new ActiveXObject("Microsoft.XMLHTTP");
						}
						catch (e)
						{
							return false;
						}
					}
				}
			}

			function Foo()
			{
				var cnt1 = 0;
				var cnt2 = 0;
				for (var i = 1; i <= 5; i++)
				{
					document.getElementById("echo").innerHTML += i + "<br />";
					Connect();

					var site = "fooy.php";
					GetServer.open("GET",site,true);
					GetServer.send(null);

					cnt1++;

					GetServer.onreadystatechange=function()
					{
						cnt2++;
						if(GetServer.readyState == 4)
						{
							document.getElementById("echo").innerHTML += GetServer.responseText;
						}
					}
				}
				document.getElementById("cnt1").innerHTML += "Count 1: " + cnt1;
				document.getElementById("cnt2").innerHTML += "Count 2: " + cnt2;
			}
		-->
	</script>
</head>
<body>
	<div id="echo"></div>
	<div id="cnt1"></div>
	<div id="cnt2"></div>
	<input type='button' value='Foo It!' onclick="Foo()">
</body>
</html>

 

 

The output of the above script when the button is pressed is this:

1
2
3
4
5
Fooy on you!
Count 1: 5
Count 2: 0

 

 

I would like the output to look like this:

1
2
3
4
5
Fooy on you!
Fooy on you!
Fooy on you!
Fooy on you!
Fooy on you!
Count 1: 5
Count 2: 5

 

 

I don't get it! Shouldn't it connect to fooy.php 5 times??

 

Furthermore shouldn't cnt2 equal at least 1! I mean, if it echoed "Fooy on you!" once, then it had to of done cnt2++; at least once.... (I did check cnt2, it is the correct number... Some how...)

 

Any ideas, suggestions?

 

Thanks!

Link to comment
Share on other sites

It seems like it may be a problem with the server being called too quickly. I'm not sure though. I'm looking around to see what I can find. Because even if this was working, the way you have it set up if should print

1

Fooy

2

Fooy

etc...

 

 

Link to comment
Share on other sites

Your XMLHTTPRequest (XHR) Object Instance is a global. (GetServer)

Every time you call Connect() you reset this Object. What happens is the XHR is canceled and a new one made - depending on browser.

 

You need to create a new Instance for each XHR you make. Let each XHR complete, before reusing the Instance, or destroying it.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.