0

I'm trying to call a function, then take the returned variable and display it on the screen within a div. However, in my current format below, the .html() gets executed simultaneously as the postGameStatistics() function. postGameStatistics() is a function that does an ajax post amongst some other actions. Is there a way to chain this?

        var fbDiv = "#fb-request-wrapper";
        var xp = postGameStatistics(fbDiv, "#loading_fb", "p2", null);
        $(fbDiv).html("Congrats! you've just earned " + xp + " XPs.");
        $(fbDiv).show();
1
  • Execute $(fbDiv).html(...) in the postGameStatistics callback. As we don't know how postGameStatistics works, this is all we can say. Commented Jul 23, 2011 at 11:33

3 Answers 3

2

Two options. I think the second one is really what you want.

1) set the optional "async" property of a jQuery ajax request to false. This will make the rest of the script wait until the request has finished before proceeding. For example:

$.ajax({
    url: http://example.com,
    type: 'POST',
    async: false,
    data: myData,
    success: function(data){
        //handle a successful request
    },
    error: function(data){
        //handle a failed request
    }
});

2) Execute the second two lines of code in the "success" callback of the $.ajax method. This is the standard jQuery way to handle AJAX requests. Note that you do not need to set async to false in this case:

$.ajax({
    url: http://example.com,
    type: 'POST',
    data: myData,
    success: function(data){
        $(fbDiv).html("Congrats! you've just earned " + data + " XPs.");
        $(fbDiv).show();
    },
    error: function(data){
        //handle a failed request
    }
});
Sign up to request clarification or add additional context in comments.

3 Comments

Using a synchronous request is almost always the wrong approach.
It works using the synchronous approach. The suggestion to embed the response message within the ajax makes sense, however, not all calls to postGameStatistics() require a response message to be posted. Since this process is always sequential, I think synchronous call would be best. Thoughts? What's the drawback?
You're going to have to offer further explanation. You write that "not all calls to postGameStatistics() require a response message to be posted." But in the example code you posted, this doesn't seem to be the case. No matter what (in the example code), fbDiv displays a message about xp points. If there are times when you're posting game statistics silently (i.e. the user doesn't need to know about them), then simply don't put anything in the success callback. If the user needs to be alerted only if something goes wrong with the posting, then you can put some conditional code in the callbacks
0

You could move the $(fbDiv).html() and $(fbDiv).show() into a callback function, that get`s called after the ajax request finished successful.

So your request would look something like:

var postGameStatistics = function(div) {
    var $div = $(div);
    $.get('get/xp', {some: "parameters"}, function(response) {
        var xp = response.xp;

        $div.html("Congrats! you've just earned " + xp + " XPs.");
        $div.show();
    });
};

Comments

0

with jQuery your can use ajax() function to make ajax calls. To achieve what you want, consider using success() or cemplete() methods under ajax() function. In success() method you can fill and show your DIV element.

More can be read here: http://api.jquery.com/jQuery.ajax/

EDIT.

A little example


$.ajax({
  url: 'http://example.com/something',
  action: 'post',
  success: function( data ) {
    $(fbDiv).html("Congrats! you've just earned " + xp + " XPs.");
    $(fbDiv).show();
  }
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.