It's a easy misunderstanding to have when you first begin to use AJAX.

Remember that the first 'A' in AJAX stands for "Asynchronous". You ask
the browser (via jQuery's ajax function) to retrieve a URL and you
give it a function that it will call with the result when it has
retrieved it.

The browser may take 50 milliseconds or 50 seconds to retrieve the
URL, you can't be sure how long.

In the mean time the browser (and your JavaScript function) continues
on its way.

So you must put all the code for handling the results in the "success"
(or "error") handler function.

Does that help?

Karl Rudd

On Feb 11, 2008 1:59 PM, saidbakr <[EMAIL PROTECTED]> wrote:
>
> Hi,
>
> I have very little knowledge about javascript. I used it with ordinary
> functions and the code of jquery may make me confused.
> I used it in a javascript function some thing like that:
>
> <script>
> function avaChk(lnk,id){
> alert('111')
> ob = document.getElementById('id');
> $.ajax({url: 'chk_user.php',type:'GET', data: id+'='+ el, cache:
> false, dataType: "script", success: function(data, textStatus){
> if (data == 0){
>
>                 alert('222');
> }
> else{
> alert('333');
> }
> },
>         error: function(x,txt,err){
>         $("#waitImg"+lnk).remove();
>         alert('Could not check...'+"\n"+'The server may down or busy. Retry
> again after a while.');
>         }
> });
> alert('444');
> }
>
> What I noticed is, when I call the function avaChk(), for me, very
> strange thing is happened. ;) I think the jQuery proffesional will
> know it:
>
> The alert sequence as I respect is 111, 222 or 333 then 444
> what happened is not like above, it was 111,444, 222 or 333 !!!
>
> I respected that the jQuery code should be executed before the
> alert(444). In real world this prevent me from using the value of data
> or return it through another function.
>
> What's wrong in my understanding and how can I overcome this problem?
>

Reply via email to