Question from an almost ajax newbbie

2 views
Skip to first unread message

david....@gmail.com

unread,
Jan 23, 2009, 7:38:12 AM1/23/09
to Prototype & script.aculo.us
First of all I'm David, from Italy.
I recently started looking into your fabolous prototype thanks to this
script: http://tetlaw.id.au/view/javascript/really-easy-field-validation

After some playing around I decided to add another validation check.
Without going into the details of the other script (which is not
relevent) I would like to ask you how can I return a variable AFTER an
Ajax request has finished.

This is the code:

function(v) {
new Ajax.Request('availability.php',
{method: 'get', parameters: {validate: v},
onSuccess: function( transport ) {
if( transport.responseText.match(/true/)) {
risultato = false;
}
else {
risultato = true;
}
}
});
return risultato;
}

The problem is that availability takes a long time to process the
response (1sec) and while he process the thing risultato has the value
of the last query.

For example: first time we have risultato = true;
so it returns: true
The second time I invoke the function i still have risultato = true
from before. Now the ajx request returns risultato = false BUT while
it goes the functions returns true.

How can I avoid this?

Thank you very much

T.J. Crowder

unread,
Jan 23, 2009, 8:08:45 AM1/23/09
to Prototype & script.aculo.us
Ciao David,

By default, Ajax requests are asynchronous, and so they get *started*
when you do "new Ajax.Requst" but then they run for a while and
complete later; meanwhile, your code that started the request
continues. So what your anonymous function above returns will be
whatever value risultato has when the function starts, since the
request hasn't completed yet. Later, at a time you cannot predict,
the value of risultato will be updated by the callbacks you've
registered.

You _can_ make requests synchronous if you like (set the asynchronous
parameter to false), but that will lock up the UI of the browser for
the entire time the request is taking pace -- which from what you've
said about availability.php non sembra una buona idea.

This might be useful:
http://proto-scripty.wikidot.com/prototype:how-to-bulletproof-ajax-requests
It's an example of an Ajax request that starts the process of
retriving a record from the server, and then displays that record (or,
of course, an error) on callback.

Basically, when working with lots of asynchronous stuff like this, you
end up modifying your logic so you don't make a function call and
expect an answer back, but instead you make a function call and expect
to handle the result in callbacks.

HTH,
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available


On Jan 23, 12:38 pm, "david.0pl...@gmail.com" <david.0pl...@gmail.com>
wrote:

david....@gmail.com

unread,
Jan 23, 2009, 9:12:54 AM1/23/09
to Prototype & script.aculo.us
I get an hint that we are 'compaesani' :-P

Anyhow.. thank you very much,I'm studying that link you gave me,
meanwhile, in my particular case setting to "syncronous" has indeed
resolved my issue.

Thank you very much for you toughtfull response


A presto

David(e)

On Jan 23, 2:08 pm, "T.J. Crowder" <t...@crowdersoftware.com> wrote:
> Ciao David,
>
> By default, Ajax requests are asynchronous, and so they get *started*
> when you do "new Ajax.Requst" but then they run for a while and
> complete later; meanwhile, your code that started the request
> continues.  So what your anonymous function above returns will be
> whatever value risultato has when the function starts, since the
> request hasn't completed yet.  Later, at a time you cannot predict,
> the value of risultato will be updated by the callbacks you've
> registered.
>
> You _can_ make requests synchronous if you like (set the asynchronous
> parameter to false), but that will lock up the UI of the browser for
> the entire time the request is taking pace -- which from what you've
> said about availability.php non sembra una buona idea.
>
> This might be useful:http://proto-scripty.wikidot.com/prototype:how-to-bulletproof-ajax-re...

eulerss

unread,
Jan 23, 2009, 10:21:34 AM1/23/09
to Prototype & script.aculo.us
my problem was very similar, i only added the next line into my
script:

asynchronous: false,

and it works fine

eulerss

On 23 ene, 08:12, "david.0pl...@gmail.com" <david.0pl...@gmail.com>
wrote:
> > > Thank you very much- Ocultar texto de la cita -
>
> - Mostrar texto de la cita -

Jonathan Rosenberg

unread,
Jan 23, 2009, 10:43:19 AM1/23/09
to prototype-s...@googlegroups.com
While this will "fix" the problem, it is bad practice to cause blocking in Javascript. Particularly so in this case, since the
poster stated that the request takes a long time to fulfill. Remember that Javascript is single-threaded.

I'm relatively new to serious Javascript programming, but every time I've made an Ajax request synchronous, I've ended up having to
change it.

My advice: spend the time to wrap your head around the Javascript execution model. You'll be grateful later.

--
Jonathan Rosenberg
Founder & Executive Director, Tabby's Place
http://www.tabbysplace.org/

T.J. Crowder

unread,
Jan 23, 2009, 12:09:37 PM1/23/09
to Prototype & script.aculo.us
I'm with Jonathan on this: The blocking (which in many browsers will
completely lock up the UI, making the browser completely
nonresponsive) makes for a bad user experience, and you can almost
always do it better using callbacks. Takes a bit more thought, but
it's worth it.
--
T.J. Crowder
tj / crowder software / com
Independent Software Engineer, consulting services available


On Jan 23, 3:43 pm, "Jonathan Rosenberg" <j...@tabbysplace.org> wrote:
> While this will "fix" the problem, it is bad practice to cause blocking in Javascript.  Particularly so in this case, since the
> poster stated that the request takes a long time to fulfill.  Remember that Javascript is single-threaded.
>
> I'm relatively new to serious Javascript programming, but every time I've made an Ajax request synchronous, I've ended up having to
> change it.
>
> My advice: spend the time to wrap your head around the Javascript execution model.  You'll be grateful later.
>
> --
> Jonathan Rosenberg
> Founder & Executive Director, Tabby's Placehttp://www.tabbysplace.org/
Reply all
Reply to author
Forward
0 new messages