Folks - my apologies first if this should have gone in the Javascript frameworks group, but it's a kind of hybrid Javascript / jQuery question so here goes!

Firstly, please excuse the fairly length code snippet below - but I felt it pertinent to include all functions involved.

I have a button on my web page that calls "initiateRun". From there, you can see what happens - waitForComplete looks to see if a process is running server side, while updateLogAndWait posts the latest output of that process to a textarea on the webpage, pauses, and then we repeat.... Or rather, this SHOULD be what happens.... In it's current form, the sequence of alerts I get is:
GMS Data **STUFF**
Process found
Waiting 1s GMS=1 << and this is then repeated forever


Bizarrely, If I add an additional alert "Looking for process" immediately after the "var wfcRequest = " in my WaitForComplete function I get:
Looking for Process
GMS Data **STUFF**
Process found
Waiting 1s GMS=1
Looking for Process
GMS Data **STUFF**
Process found
Waiting 1s GMS=1

and so on, until my process has exited, at which point I get:
Looking for Process
GMS Data (nothing)
NO Process found
Waiting 1s GMS=1
Looking for Process
GMS Data (nothing)
NO Process found
Waiting 1s GMS=1

and so on... for ever again.

So - I'm stumped as to a) why I need the additional alert in order to "correctly resubmit" the wfcRequest jQuery, and b) why, when the process that getMarsStatus looks for has exited, I still end up caught in the loop?!?

Can anyone suggest anything?

Thanks!!


Code:
    function initiateRun()
    {
        var logbox = document.getElementById("marsoutput");
        logbox.value = "";
        var request = $.get("initiateMars") // routes to a call to runSuites
        request.success(function(data, textStatus, jqXHR){
            waitForComplete();
            getMarsLog();
        })
        request.error(function(jqXHR, textStatus, errorThrown) {
            alert ("IR err: " + errorThrown);
        })
        sleep (3000)
    }

    function getMarsLog()
    {
        var gmlRequest = $.get("getMarsLog"); // routes to a call to getMarsOutput
        gmlRequest.success(function(data, textStatus, jqXHR){
            var logbox = document.getElementById("marsoutput");
            logbox.value = data;
        })
        gmlRequest.error(function(jqXHR, textStatus, errorThrown) {
            alert ("GML err: " + errorThrown);
        })

    }

    function waitForComplete()
    {
        var wfcRequest = $.get("getMarsStatus"); // routes to a call to checkForMarsProcess
        wfcRequest.success(function(data, textStatus, jqXHR){
            alert("GMS Data: " + data)
            if (data == "") {
                alert("NO Process found");
                updateLogAndWait(0);
                return;
            }
            else{
                alert("Process found");
                updateLogAndWait(1);
            }
        })
        wfcRequest.fail(function(jqXHR, textStatus, errorThrown) {
            alert ("GMS err: " + errorThrown);
        })
    }

    function updateLogAndWait(GMS)
    {
        while (GMS == 1)
        {
            alert("Waiting 1s, GMS = " + GMS);
            sleep(1000);
                getMarsLog();
            waitForComplete()
        }
        alert ("Exiting updateLogAndWait Loop")
    }