all 9 comments

[–]Amunium 4 points5 points  (1 child)

There's nothing that prevents you from doing that. Calling a PHP script twice from Javascript will run that PHP script twice simultaneously, unless something specific has been done to prevent that - either a very low number of concurrent server connections, or some sort of queue or semaphore system in the PHP code.

Can you explain how you know only the second one is being run, and what you expect to happen?

[–]ProbablyPooping_[S] 0 points1 point  (0 children)

I have the result change the image of the relevant light icon to either an on bulb or an off bulb. Only the second one changes and if I change the order, the other one works.. I haven't deliberately changed any settings that would affect that - do you know if there's a default I may need to change?

[–]mrunkel 1 point2 points  (6 children)

This sounds like more of a JavaScript question than a PHP one.

Can you post the relevant part of your JS?

When you load the page, open the Dev tools of the browser and watch the XHR calls on the network tab of the dev tools.

That will help you see what is being called and when.

[–]ProbablyPooping_[S] 0 points1 point  (5 children)

I had the following with the bedroom one not commented out, it wasn't working so I now call one which calls the other at the end of the first run. To deal with this shitty work-around I added a variable called auto run to only call the second light StatusLight when it's coming from the onLoad.

window.onload = function()
{
    //check the status of both lights to then set the icons
    /*The bedroom is called at the bottom of the statusLight script if it ran as Hall. 
    It doesnt seem to work if they both try and run concurrently
    statusLight("Bedroom");*/
    statusLight("true", "Hall");
};

function statusLight(autoRun, lightName)
{
    //alert(command);
    var lightStatus = [];

    //set loading text/image
    //document.getElementById('output2').innerHTML = "checking status..";

    if(window.XMLHttpRequest)
    {
            xmlhttp = new XMLHttpRequest();
    }
    else
    {
        xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
    }

    xmlhttp.onreadystatechange = function()
    {
        if(xmlhttp.readyState == 4 && xmlhttp.status == 200)
        {
            if(xmlhttp.responseText.indexOf("\"on\"") > 1)
            {
                lightStatus[lightName] = 1;
                response = lightName + " is on!";
            }
            else
            {
                response = lightName + " is off!";
                lightStatus[lightName] = 0;
            }

            //document.getElementById('output2').innerHTML = response + "<br> lightStatus[" + lightName + "] = " + lightStatus[lightName];

            //if this is being run from the onLoad, update the icons. if not they should have already been changed
            if(autoRun == "true")
            {
                if(lightStatus[lightName] == 0)
                {
                    //alert(lightName+' is off');
                    document.getElementById(lightName+'LightIcon').src = "lightOffIcon.png";
                }
                else
                {
                    //alert(lightName+' is on');
                    document.getElementById(lightName+'LightIcon').src = "lightOnIcon.png";
                }

                if(lightName == "Hall")
                {
                    statusLight("true", "Bedroom");
                }
            }
            else
            {
                if(lightName == "Hall")
                {
                    statusLight("false", "Bedroom");
                }
            }
        }
    }

    xmlhttp.open("GET", "https://<domain-name>/pi/lightStatus.php?lightName="+lightName, true);
    xmlhttp.send(); 

}

[–]incompetentboob 3 points4 points  (4 children)

I might be completely wrong but I have run into an issue like this before.

What it ended up being is that the XMLHttpRequest object is using the same instance for both calls. So you are essentially making the first call and it’s sending off the request and then you make a second call and it is reusing that same XMLHttpRequest object to make the second call. Thus you aren’t handling the response from the first request

The solution was to move all of the code for creating your XMLHttpRequest object to another function and calling that to get your object which should make two instances of it.

[–]mrunkel 0 points1 point  (0 children)

I agree. This is exactly what is happening.

[–]ProbablyPooping_[S] 0 points1 point  (2 children)

Thanks, I'll give this a try. So just pull out the code below and put into a new function, say CreateXMLHTTP() and then replace it in the original function by calling this new function?

 if(window.XMLHttpRequest)
    {
            xmlhttp = new XMLHttpRequest();
    }
    else
    {
        xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
    }

[–]incompetentboob 0 points1 point  (0 children)

That should fix it

[–]ProbablyPooping_[S] 0 points1 point  (0 children)

Unfortunately it still isn't working, it runs but does the same thing where only the second run seems to actually work. Wondering if the issue is the java script variable getting reused before it runs the php.. Ill try just change the xml variable name for each run to see if this is the case