[web] problem with javascript global variable

Started by
6 comments, last by Wan 14 years, 1 month ago
i'm having a problem with a global &#106avascript variable (myStringList). initially the variable is undefined, but later i populate it with a list of strings using .split(). i use myStringList through a few functions with no problems until it backs out of the functions. here's the problem and it's quite odd. using the code below: alert("list contains : " + myStringList); alert("list contains : " + myStringList); i come out with two different outputs even though the lines of code are exactly the same and executed one after the other. when the first line is executed, the output is that of what myStringList is originally initialised with at the top of the script. after the second line is executed, it outputs the strings i had populated it with, and remains consistent after that point. for example: var myStringList = "NONE SHALL PASS!"; // global variable window.onload = function() { getStatus(); // myStringList will be populated here // with "hp,90,100,10,3,mp,50,75,15,4,ap,15,100,25,2" // using a .split() alert("list contains : " + myStringList); alert("list contains : " + myStringList); } the first alert box will say "list contains : NONE SHALL PASS!" and the second alert box will say "list contains : hp,90,100,10,3,mp,50,75,15,4,ap,15,100,25,2" and remains constant thereafter as it should have been anyways. does anyone have an idea why it is behaving in this manner?
Advertisement
I can't see why that would be occuring.. can you post some of the actual code in question, namely getStatus?

You don't have any other alerts around causing the weird alerting behaviour, do you?
function getStatus()
{
xmlhttp=GetXmlHttpObject();

if(xmlhttp==null)
{
alert("Browser does not support HTTP Request");
return;
}

var url="status.php";
xmlhttp.onreadystatechange=changeStatus;
xmlhttp.open("GET",url,true);
xmlhttp.send(null);
}

function changeStatus()
{
if (xmlhttp.readyState==4)
{
//alert(xmlhttp.responseText);
myStringList = xmlhttp.responseText.split(',');

display();
}
}

getStatus() is nothing more than an ajax call to status.php which returns the string "hp,90,100,10,3,mp,50,75,15,4,ap,15,100,25,2" and then split into separate values so that myStringList can then be accessed like a list. display() does not change the list at all, but displays the appropriate values in a web page for me with no trouble. i've even tested an alert("list contains : " + myStringList); at the bottom of changeStatus() where it displays "hp,90,100,10,3,mp,50,75,15,4,ap,15,100,25,2" appropriately. it's when it comes out of getStatus() that suddenly myStringList reverts back to "NONE SHALL PASS!" or whatever else i initialize it as. it only seems to change back to "hp,90,100,10,3,mp,50,75,15,4,ap,15,100,25,2" after the first alert(), which is extremely weird...
Ah, but it's an asynchronous call, meaning that the browser won't wait for the server response before it executes the rest of the script. Why it always shows the old value the first time, and the new value the second time, I don't know. It might be a coincidence, or it might have something to do with how the browser handles the separate threads.

In any case, you can only be sure of the list's value after the readyState has been set to 4, as you check in your changeStatus callback.
Quote:Original post by Wan
Why it always shows the old value the first time, and the new value the second time, I don't know. It might be a coincidence, or it might have something to do with how the browser handles the separate threads.

The XMLHTTP request probably has time to complete while the first alert is being displayed.
Quote:Original post by mattd
Quote:Original post by Wan
Why it always shows the old value the first time, and the new value the second time, I don't know. It might be a coincidence, or it might have something to do with how the browser handles the separate threads.

The XMLHTTP request probably has time to complete while the first alert is being displayed.

Yeah, I was wondering why he was able to reproduce the exact same situation (one old alert, one new alert) every single time. If a user discards the first message box really fast, he should sometimes be able to 'beat' the server round trip, and have the second alert display the old message too. I assume the request is send to the localhost and requires very little time to return then I guess.
i had thought of that originally, and had tried placing a wait function before the alerts. i've set a timer to wait as long as 10 seconds, but the same problem occurred.
Quote:i had thought of that originally, and had tried placing a wait function before the alerts. i've set a timer to wait as long as 10 seconds, but the same problem occurred.

Then you might as well make a synchronous request by setting the third parameters to false.
xmlHttp.open("GET", url, false);

But rather than suspending the script and waiting for a status update to arrive, why not have it run in the background?
var updateStatus = function (stateChanged){  var xmlHttp = getXmlHttpObject();  xmlHttp.onreadystatechange = onReadyStateChange;  xmlHttp.open("GET", "status.php", true);  xmlHttp.send(null);    function onReadyStateChange()  {    if (this.readyState == 4)    {      stateChanged(this.responseText.split(','));    }  }}var stateChanged = function (state){  alert(state);}updateStatus(stateChanged);

This topic is closed to new replies.

Advertisement