Hi,

You can't busy-wait (your `do { } while (!bDone);` loop) in browser-
based JavaScript (nor do you want to).

The best thing to do, by far, is embrace the asynchronous, callback
nature of ajax and use the `onSuccess` callback to trigger the next
thing you want to do, e.g.:

    function test() {
        getUserImage(25016275, function(result, value) {
            if (result) {
                alert(value);
            }
        });
    }
    function getUserImage(userID, callback) {
        new Ajax.Request('users.json', {
            method: 'get',
            requestHeaders: { Accept: 'application/json' },
            onSuccess: function (transport) {
                var obj = transport.responseText.evalJSON();
                var i = 0;
                for (i = 0; i < obj.length; i++) {
                    if (obj[i].id == userID) {
                        callback(true, 'Gold'); // Found
                        return;
                    }
                }
                callback(false); // Not found
            }
        });
    }

When you call `test`, it will return immediately, but some time later
the callback function within `test` will get called (assuming the ajax
request didn't fail, since the above only uses `onSuccess`).

You _could_ use a *synchronous* ajax request by setting the
`asynchronous` option[1] to `false` in your Ajax.Request call, but
this is a Really Bad Idea. On most browsers, doing that will
completely lock up the UI of the browser (not just your page, the
entire browser) during the time your ajax call is being processed.
Locking up the UI for 1-30 seconds is a Bad Thing(tm). :-)

[1] http://api.prototypejs.org/ajax/

HTH,
--
T.J. Crowder
Independent Software Engineer
tj / crowder software / com
www / crowder software / com

On Jan 23, 2:57 pm, Deano <deanale...@gmail.com> wrote:
> Hi am having problems returning a value from an ajax request.
>
>     var TESTReturnable;
>     var bDone = false;
>
>     function test() {
>         getUserImage(25016275);
>         do { } while (!bDone);
>         alert(TESTReturnable);
>     }
>
>     function getUserImage(userID) {
>         new Ajax.Request('users.json', {
>             method: 'get',
>             requestHeaders: { Accept: 'application/json' },
>             onSuccess: function (transport) {
>                 var obj = transport.responseText.evalJSON();
>                 var i = 0;
>                 for (i = 0; i < obj.length; i++) {
>                     if (obj[i].id == userID) {
>                         TESTReturnable = 'Gold';
>                         bDone = true;
>                     }
>                 }
>             }
>         });
>     }
>
> That is my code assume that the obj[i] is set to true.
> html contains
>
> <body onload="test()"> however the Javascript alert will either not
> show or display undefined;  How can I get this function to return a
> value as I have multiple requests to load a page.
>
> Regards

-- 
You received this message because you are subscribed to the Google Groups 
"Prototype & script.aculo.us" group.
To post to this group, send email to prototype-scriptaculous@googlegroups.com.
To unsubscribe from this group, send email to 
prototype-scriptaculous+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/prototype-scriptaculous?hl=en.

Reply via email to