Web Hosting Forums

Results 1 to 7 of 7

This is a discussion on How to remove the A from AJAX? in the Hosting Talk & Chit-chat forum
I'm using prototype to do an AJAX request. I want the script that follows this request to wait until the request is done before executing. ...

  1. #1
    Loyal Client
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    135

    How to remove the A from AJAX?

    I'm using prototype to do an AJAX request. I want the script that follows this request to wait until the request is done before executing.

    This is the core script:

    function myScript() {
    c=0;
    new Ajax.Request('test.php', {method: 'get', onComplete: function(requestResponse) {alert((++c)+' ajax script done')}, onException: function() {alert('exception')}});
    alert((++c)+' non ajax script done');
    }
    Now, I want the message 'ajax script done' to precede the message 'non ajax script done'. As you can see for yourself here, it doesn't.

    I've tried setting asynchronous to false, but this seems to stop the request dead in it's tracks. (All the request is currently returning is the number 1, so there's no problem there).

    Is this even possible?

  2. #2
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    5,884
    Try something like this (untested, changes in red, code expanded for better readability):

    Code:
    var done;
    function myScript() {
        c=0;
        done = false;
        new Ajax.Request('test.php', {
            method: 'get',
            onComplete: function(requestResponse) {
                done = true;
                alert((++c)+' ajax script done')
            }, 
            onException: function() {
                done = true;
                alert('exception')
            }
        });
        checkDone();
        
    }
    
    function checkDone() {
        if(!done) {
            window.setTimeout("checkDone", 10);
        }
        else {
            alert((++c)+' non ajax script done');
        }
    }
    In the above example, I use a boolean variable "done" to track if the AJAX reguest has finished. I then call the checkDone function to see if done = true. If not I set a timer that calls checkDone again in 10ms.

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

  3. #3
    Loyal Client
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    135
    Thanks for that.

    The 'else' of the checkDone function is never activated, so my 'non ajax script' never runs.

    I was under the impression setting asynchronous to false would achieve the effect I'm after. Why isn't it?
    Last edited by uprightdog; 06-07-2006 at 04:07 PM.

  4. #4
    the Windlord Gwaihir's Avatar
    Join Date
    Jun 2002
    Posts
    2,570
    Might it be something minor, a typo or something like asynchronous: 'false' instead of asynchronous: false?

    I'm a bit confused by seeing those onComplete and onException functions definded there in place. I don't know if it matters (I guess it shouldn't), but I'm used to seeing only function names there, of function defined seperately.

    BTW: I assume you know the 'main' manual page for this thing: http://www.sergiopereira.com/articles/prototype.js.htm


    That said, why do you want it synchronous? Can't you just call that non-ajax stuff you want done from your onComplete function?
    Regards,

    Wim Heemskerk
    ---
    Visit MeCCG.net - Cardgaming in J.R.R. Tolkien's Middle-earth
    And Gwaihir.net - The Middle-earth CCG store

  5. #5
    Loyal Client
    Join Date
    Aug 2004
    Location
    Canada
    Posts
    135
    Quote Originally Posted by Gwaihir
    Might it be something minor, a typo or something like asynchronous: 'false' instead of asynchronous: false?
    I've tried several permutations, I'm pretty sure asynchronous: false is the correct one.

    Quote Originally Posted by Gwaihir
    I'm a bit confused by seeing those onComplete and onException functions definded there in place. I don't know if it matters (I guess it shouldn't), but I'm used to seeing only function names there, of function defined seperately.

    BTW: I assume you know the 'main' manual page for this thing: http://www.sergiopereira.com/articles/prototype.js.htm
    Inline functions like that are fine, I've done it that way before. You assume correctly about the manual.

    Quote Originally Posted by Gwaihir
    That said, why do you want it synchronous? Can't you just call that non-ajax stuff you want done from your onComplete function?
    In the full script this ajax call is part of a library, and it really needs to be self-contained.

    I've managed to achieve the effect I'm after using setInterval, but it seems rather a hack when, as I understand it, asynchronous should do it. Maybe asynchronous in prototype is buggy.

  6. #6
    Community Leader jason's Avatar
    Join Date
    Sep 2001
    Location
    Rochester, NY
    Posts
    5,884
    I often refer to Apple's developer reference on AJAX http://developer.apple.com/internet/...lhttpreq.html), as it is one of the better references I've seen. Here's what they have to say about async vs. sync:

    An important optional third parameter is a Boolean value that controls whether the upcoming transaction should be handled asynchronously. The default behavior (true) is to act asynchronously, which means that script processing carries on immediately after the send() method is invoked, without waiting for a response. If you set this value to false, however, the script waits for the request to be sent and for a response to arrive from the server. While it might seem like a good idea to wait for a response before continuing processing, you run the risk of having your script hang if a network or server problem prevents completion of the transaction. It is safer to send asynchronously and design your code around the onreadystatechange event for the request object.
    I've never used the JS library that you are using (although it looks like something worth checking out), so I can't comment if that is causing a bug. AFAICT, using anonymous functions for the onComplete and onException handlers should not be a problem.

    --Jason
    Jason Pitoniak
    Interbrite Communications
    www.interbrite.com www.kodiakskorner.com

  7. #7
    the Windlord Gwaihir's Avatar
    Join Date
    Jun 2002
    Posts
    2,570
    Rather incomplete advise from Apple there; unless you build in a system that retries the failing request, whatever you are doing effectively still hangs. This might make sense if it's some applet that can be missed in a near forgotten corner of your page, but if it's a main part the user will have to refresh anyway to get out of the deadlock.

    Prototype and the effects library scriptaculous that's built on it are definately worth checking out.

    It's also still under active and serious development, so perhaps you should take it to their own feedback systems?

    I can imagine you don't like the setInterval solution, although it would enable a retry too, in case the reply for some reason never comes. Where it comes to Prototype and intervals / polling, I am dissapointed that it doesn't really support unsetting an interval again.
    Regards,

    Wim Heemskerk
    ---
    Visit MeCCG.net - Cardgaming in J.R.R. Tolkien's Middle-earth
    And Gwaihir.net - The Middle-earth CCG store

Bookmarks

Posting Permissions

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