2011-02-09 108 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