www.webdeveloper.com
Results 1 to 3 of 3

Thread: Ongoing callback confusion!

  1. #1
    Join Date
    Mar 2014
    Location
    UK
    Posts
    4

    Ongoing callback confusion!

    Hi All,

    I thought I had things sussed after the answer in http://www.webdeveloper.com/forum/sh...allback-jQuery - but my code still isn't working as previously thought. To expand on my previous code / question:

    Code:
            function processThisSuite(suite, suitenumber)
            {
                //var request = $.post("initiateMars", {thisSuite: selectedSuites[i], suiteNo: i}) // routes to a call to runSuites
                var request = $.post("initiateMars", {thisSuite: suite, suiteNo: suitenumber}) // routes to a call to runSuites
                request.done(function(data, textStatus, jqXHR){
                    waitForComplete();
                    getMarsLog();
                })
                request.error(function(jqXHR, textStatus, errorThrown) {
                    alert ("IR err: " + errorThrown);
                })
                alert ("done")
            }
    
        function getMarsLog()
        {
            var gmlRequest = $.get("getMarsLog"); // routes to a call to getMarsOutput
            gmlRequest.done(function(data, textStatus, jqXHR){
                console.log("Updating log")
                var logbox = document.getElementById("marsoutput");
                logbox.value = data;
                logbox.scrollTop = logbox.scrollHeight;
            })
            gmlRequest.error(function(jqXHR, textStatus, errorThrown) {
                alert ("GML err: " + errorThrown);
            })
    
        }
    
        function waitForComplete()
        {
            var wfcRequest = $.get("getMarsStatus"); // routes to a call to checkForMarsProcess
            wfcRequest.done(function(data, textStatus, jqXHR){
                if (data == "") {
                    setTimeout(function(){
                        alert("No data found")
                        enableButton();
                        updateLogAndWait(0);}, 1000);
                }
                else{
                    setTimeout(function(){
                    alert("Data found")
                    disableButton("RUNNING");
                    updateLogAndWait(1);}, 1000);
                }
            })
            wfcRequest.error(function(jqXHR, textStatus, errorThrown) {
            })
            alert ("exiting WFC")
        }
    
        function updateLogAndWait(GMS)
        {
            if (GMS == 1)
            {
                console.log("Update and Wait")
                getMarsLog();
                waitForComplete()
            }
        }
    (Note that functions such as disableButton etc not quoted here to keep it simple!)

    Very simply - the call to waitForComplete in proecssThisSuite is executing synchronously - I need it to be asynchronous... processThisSuite will itself be called a number of times for a number of suites - which will be processed on the back end... And the back end processing of the suites does NOT support concurrent suite processing - so I need to ensure I only process one at a time...

    Can someone please give an idiot proof guide (I'll let you guess who the idiot in question is!!!) as to how to (I believe using callbacks) make the call to WaitForComplete asynchronous - ie, call it, and wait for it to exit before continuing?

    Thanks!

  2. #2
    Join Date
    Mar 2005
    Location
    Behind you...
    Posts
    957
    I think there is some confusion about terminology here as well. Asynchronous request simply initiate a process and allow everything else to continue. Synchronous request prevent further request from starting until the initial synchronous request has completed.

    There are some other things I would like to clear up first. Looking at your code and based on what you've said, essentially you want to make sure you do not run processThisSuite() until the previous instance of that has completed. How are you calling processThisSuite() in the first place?

    The jQuery .ajax() method (vs .post()) allows you to specify the async property, setting it to false to make the request synchronous. However this really just means that your page essentially sits there 'loading' until the request hits it's 'complete' state. But you seem to be also running functions that check the status of this process and update the results to a log. If that request were synchronous then (as far as I know) you wouldn't be getting real-time updates on the status. What I think needs to be changed instead is the way your processThisSuite() function gets called. If you want to prevent multiple instances of that being called at the same time then you'd have to add logic to where the function is being called and adjust it so that it uses your logging methods to verify that a previous instance has completed.
    "Given billions of tries, could a spilled bottle of ink ever fall into the words of Shakespeare?"

  3. #3
    Join Date
    Mar 2014
    Location
    UK
    Posts
    4
    At the moment, design is very much in limbo, so there is no definitive answer to your question of "how am I calling processThisSuite" - but basically the intention was: create an array of suites (based on the options selected in a multiple select_tag - and loop through this array, calling processThisSuite.

    Having read your thoughts ... or rather comments(!) ... I think what I need to do is:
    Have just a single parameter "n" for processThisSuite. Initial call to the function will be 0.
    Inside processThisSuite, add a call to the function that gets the array of selected list items.
    Get the nth element of this array.
    Do my initateMars jQuery as now, and call waitForComplete, passing n in as a parameter.
    modify waitForComplete, so that in the event of "No data found" - call processthisSuite(n+1)

    Or even pass "processThisSuite(n+1) as a "callback" Parameter to waitForComplete, then add "callback();" after my "No data found / enable button"?

    I think!?!?

    Oh, and while I'm here - I think "billions of tries" might be optimistic. An infinite number of tries, and your bottle of ink would eventually get there ;-)

    Thanks again for your help!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles