SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The XMLHttpRequest Object.

    See the description of the send method in Table 1

  3. #3
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Pepejeria
    The XMLHttpRequest Object.

    See the description of the send method in Table 1
    Okay... don't really see how that helps me?

  4. #4
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    64 Post(s)
    Tagged
    0 Thread(s)
    Code:
    new Ajax.Request('test.php', {
    	method: 'get', 
    	onComplete: function(requestResponse) {
    		alert((++c) + ' ajax script done');
    	},
    	onException: function() {
    		alert('exception');
    	},
    	asynchronous: false
    });

  5. #5
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe
    Code:
    new Ajax.Request('test.php', {
    	method: 'get', 
    	onComplete: function(requestResponse) {
    		alert((++c) + ' ajax script done');
    	},
    	onException: function() {
    		alert('exception');
    	},
    	asynchronous: false
    });

    As I said, I've tried setting asynchronous to false, but this seems to stop the request dead in it's tracks. I don't know why.

  6. #6
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    17,184
    Mentioned
    191 Post(s)
    Tagged
    2 Thread(s)

    asynchronous

    Quote Originally Posted by uprightdog
    seems to stop the request dead in it's tracks
    How long did you wait? Using asychronous "freezes" the script until it's done. If the request takes time for various reasons (slow server response, slow transfer) you may have to wait up to 3 minutes or so.

  7. #7
    SitePoint Zealot
    Join Date
    Dec 2004
    Location
    Canada
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague
    How long did you wait? Using asychronous "freezes" the script until it's done. If the request takes time for various reasons (slow server response, slow transfer) you may have to wait up to 3 minutes or so.
    When asynchronous is true, the request takes less than a second.
    When it's false, the request appears to not do anything - the script marches onto the next bit.

    See for yourself: http://www.begtodiffer.co.uk/test/


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
  •