In general, it's best to avoid synchronous ajax requests.

You can invoke functions from within your callback, but you'll need to
change the way you're doing it, because JavaScript's "this" variable in your
example below isn't referring to the object you want -- it's referring
instead to the object literal being passed to jQuery.ajax.

You have a couple of options: A) If the function you're calling is only used
in the context of this callback, you can move the functionality directly
into the callback function itself; or B) If that's not an option because the
function also needs to be called from elsewhere, you'll need to refer to the
function in a way that doesn't rely on using "this" from within the
callback. For example:

var myObj = this;

jQuery.ajax({
  ...
  success: function() {
    myObj.myFunction();
  ...
  }

or

var myFunction = this.function;

jQuery.ajax({
  ...
  success: function() {
    myFunction();
 ...
  }

Hope this helps.

- Gray

On Wed, Nov 19, 2008 at 10:17 AM, Douglas Nogueira <[EMAIL PROTECTED]
> wrote:

> How can i change the network call to sync? There is others consequences of
> this change?
>
> When you say: "So, all the work you want to do, must be done inside your
> callback
> function..."
> I cant do that. I cant call any function from the inside of my callback
> function. Doesnt work!
> Like this:
>
> jQuery.ajax({
>   type: "get",
>   url: url,
>   data: params,
>   dataType: "text",
>   success: function(response) {
>      this.myfunction();
>    }
> });
>
>
> 2008/11/18 Fernando Takai <[EMAIL PROTECTED]>
>
>
>> Douglas,
>>
>> That's because success function is an async callback -- it runs just
>> after the network call.
>> When you use a callback you don't have access to the this pointer, since
>> you are not inside your own object.
>>
>> It works like this
>>
>> aux = 0
>> network call (after this call returns, i will call your callback)
>> displayMessage
>> network call returned, displayMessage
>>
>> So, all the work you want to do, must be done inside your callback
>> function - or, change the async to sync.
>>
>> Cheers.
>>
>> On 11/18/08 5:22 PM, Douglas Nogueira wrote:
>> > I have this:
>> >
>> > aux=0;
>> >
>> > jQuery.ajax({
>> > type: "get",
>> > url: url,
>> > data: params,
>> > dataType: "text",
>> > error: function(msg) {
>> > alert("problem");
>> > },
>> > success: function(response) {
>> > if(response)
>> > if(response==1){
>> > aux=response;
>> >                           displayMessage(aux); /_(1)_/
>> >                      }
>> >           }
>> >  });
>> >
>> > displayMessage(aux); _/(2)/_
>> >
>> >
>> > When i do the first displayMessage(aux) he shows 1, but when i do the
>> > second he shows 0.
>> > And i cant call any function inside of my sucess function...
>> > What can i do to set the aux=1 if the response=1??
>> >
>> > -----
>> > Doug
>> >
>> > >
>>
>> --
>> Fernando "Takai"
>> http://flickr.com/photos/supeertakai
>> http://fernandotakai.jaiku.com
>>
>> Get Ubiquity: https://wiki.mozilla.org/Labs/Ubiquity/
>>
>>
>>
>>
> >
>

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"ubiquity-firefox" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/ubiquity-firefox?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to