Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Issues with Callback / jQuery?

    Hi All,

    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")
        }

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Try using .done() instead of .success()
    Code:
    wfcRequest.done(function(data, textStatus, jqXHR){ ....
    Do the same for the rest.

    From jQuery doc:
    The jqXHR.success(), jqXHR.error(), and jqXHR.complete() callback methods introduced in jQuery 1.5 are deprecated as of jQuery 1.8. To prepare your code for their eventual removal, use jqXHR.done(), jqXHR.fail(), and jqXHR.always() instead.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #3
    New to the CF scene
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks GlennGV - Sadly, changing from success to done made no difference. However, I did find the answer elsewhere - the while loop in updateLogAndWait needed to be a If statement instead Also replaced the Sleep function I had written, with calls to GetTimeout

    All the best

    Steve


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •