views:

110

answers:

1

I'm making a small Adobe AIR app (my first) using HTML+Javascript. I need to run more than one asynchronous data request, but the second one didn't seem to be firing (note that the requests were not run concurrently originally). I tried stripping the program down to the bare minimum that exhibited problems, and at first only the first request fired, but then things got strange. Code and output follows:

<html> 
<head>
    <script type="text/javascript" src="AIRAliases.js"></script> 
    <script type="text/javascript">         
        function download(page) {
            var url = "http://en.wikipedia.org/w/api.php?action=parse&amp;format=xml&amp;page=" + page;
            xmlhttp = new XMLHttpRequest();
            xmlhttp.open("GET", url,true);
            xmlhttp.onreadystatechange=function(){
                if (xmlhttp.readyState == 4) {
                    air.trace("Done");
                }
            }
            xmlhttp.send(null);
        }

        function appLoad() { 
            download("Main Page");
            download("Main Page");
        }  
    </script> 
</head> 
<body onLoad="appLoad()"> 
</body> 
</html>

Expected output:

Done
Done

Actual output:

C:\AIRSDK\apps\HelloWorld>adl HelloWorld-app.xml
Done
C:\AIRSDK\apps\HelloWorld>adl HelloWorld-app.xml
Done
C:\AIRSDK\apps\HelloWorld>adl HelloWorld-app.xml
Done
C:\AIRSDK\apps\HelloWorld>adl HelloWorld-app.xml
Done
Done
Done
Done
Done
Done
Done

Anyone seen anything like this before?

+1  A: 

Simple answer, you shouldn't re-use xmlhttprequest objects (even if you don't realise you are because you're a complete noob at Javascript).

This line:

            xmlhttp = new XMLHttpRequest();

Should be:

            var xmlhttp = new XMLHttpRequest();
ZoFreX

related questions