roger
roger

Reputation: 1

Global variable doesn't get assigned the value

I can't figure this out.

I have a global variable named id.

When I call the function below id doesn't return the value of msg in less I use the bottom alert('inside bottom4 (id) =' + id). If I comment out the alert, id is the value of the global variable before calling the function.

oh btw, alert('after assignment=' + id); has the correct return value.


var id = 0;

savetodb(obj);

alert(id)

function savetodb(lcalEvent) {
    var ThingID = parseInt(lcalEvent.userId);

    $.ajax({
        type: "GET",
        url: "res_update.asp",
        data: "id=" + lcalEvent.id,
        success: function (msg) {
            if (msg.indexOf('following') > 0) {
                Notices.show('error', msg, { duration: 1000 });
                $("#calendar").weekCalendar("refresh");
            } else {
                if (msg != '0') {
                    id = msg++;
alert('after assignment=' + id);
                }
                Notices.show('notice', 'Your reservation has been saved');
            }
        },
        error: function (msg) {
            alert("Data Error: " + msg);
        }
    });

alert('inside bottom4 (id) =' + id)
}

Upvotes: 0

Views: 324

Answers (1)

SLaks
SLaks

Reputation: 887499

AJAX is asynchronous.

You success callback runs some time after the rest of your code.

Upvotes: 3

Related Questions