Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Hello guys this is my very first post in this forum.

I have this function

function TestePresenca(){
var msg = ''
$('#teste tbody tr').each(function(){
    var MemberId;
    var ClassId;
    var PresenceDate;
    var OClasse;

    var DataPre = $('#data_presenca').val();
    var TotBiblia = $('#tot_biblia').val();
    var TotOferta = $('#tot_ofertas').val();

    $(this).find('td').each(function(){
        if($(this).index() == 0){
            MemberId = $(this).text();
        }

        if($(this).index() == 3){
            $(this).closest('tr').find("select").each(function(){
                ClassId = this.value;
            })
        }

        if($(this).index() == 4){
            OClasse = $(this).text();
        }           
    });

    $.ajax({
        type: 'POST',
        url: BASE + '/save_list_presence',
        data: {pMemberId: MemberId, pClassId: ClassId, pOClasse: OClasse, pDataPre: DataPre, pTotBiblia: TotBiblia, pTotOferta: TotOferta},
        success: function(retorno){
            msg += retorno;
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert('Erro!');
        }
    });
});
alert(msg);}

My problem is the "alert(msg)" fires without a message, in debug time I see its fire then the each and ajax execute after and the "msg" variable are pupulated at the end, but the alert was fired before.

Anyone have any idea how can I fix it?

Thanks in advance.

share|improve this question
classic misunderstanding of async :P – Travis J Apr 18 at 19:19
that is because, that's an asynchronous call – pXL Apr 18 at 19:20
Ajax is asynchronous. you can't fix it without making it synchronous(which is a terrible idea). – Kevin B Apr 18 at 19:20

2 Answers

up vote -1 down vote accepted

Try to use this async: false in your ajax call:

$.ajax({
        type: 'POST',
        url: BASE + '/save_list_presence',
        async: false,
        ...

From the jQuery.ajax() API Docs

By default, all requests are sent asynchronously (i.e. this is set to true by default). If you need synchronous requests, set this option to false. Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation. Note that synchronous requests may temporarily lock the browser, disabling any actions while the request is active.

share|improve this answer
This isn't really a solution. – Blender Apr 18 at 19:25
This worked like a charm. Thanks. But if not the best solution can someone point the right way? – Márlon Etiene Apr 18 at 19:31
I have dicided to change the function I'll create a new function to make a json and will send this json to my php code then i'll make the loop in php and return my entire message to jQuery show it to me. – Márlon Etiene Apr 19 at 2:15

AJAX is asynchronous - the code below it may run before the AJAX call finishes.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.