2011-02-09 90 views
0

我无法弄清楚。全局变量未分配值

我有一个名为id的全局变量。

当我调用下面的函数时,不会返回msg的值,我使用底部的alert('inside bottom4 (id) =' + id)。如果我注释掉警报,则id在调用该函数之前是全局变量的值。

oh btw,alert('after assignment=' + id);具有正确的返回值。


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

回答

3

AJAX是异步

success回调会在您的其他代码执行一段时间后运行。

+0

谢谢你。 – roger 2011-02-09 16:16:34