简体   繁体   中英

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)
}

AJAX is asynchronous .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM