[問題] javascript 全域變數的問題
最近在玩 Google Analytics Data API,卡在一個傳值的地方,
是用Javascript寫的,
我宣告一個 aa 要來存放 getAcc() 取到的值,但是在 getAcc() 裡都取不到 aa 的值,
程式碼摘要:
var myService;
myService = new google.gdata.analytics.AnalyticsService('my-test-v0.1');
var aa;
function getAcc() {
var profileFeedUri;
profileFeedUri =
"https://www.google.com/analytics/feeds/datasources/ga/accounts"
myService.getManagementFeed(profileFeedUri, print, handleError);
alert(aa); //這邊讀不到東西,顯示 undefined
}
function print(result) {
aa = result; //result是我要的東西
alert(aa); //這邊讀的到
}
是不是因為getManagementFeed是非同步的關係呢?
因為會先跳出 getAcc() 裡的alert(aa),
請問要怎麼做才能讓 getAcc() 回傳 result,
或者是要怎麼在 getAcc() 裡得到 result 呢?
麻煩各位大大解答了,謝謝!
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 220.132.189.217
→
03/15 05:03, , 1F
03/15 05:03, 1F
→
03/15 07:55, , 2F
03/15 07:55, 2F
→
03/15 07:55, , 3F
03/15 07:55, 3F
推
03/15 12:39, , 4F
03/15 12:39, 4F
謝謝大大提醒,
我在getAcc() 使用 setTimeout("alert(aa)",1000) 可以讀到aa了,
getManagementFeed 這個方法好像沒有提供同步的選項,
好像只能自己使用setTimeout來做處理
※ 編輯: On1earth 來自: 220.132.189.217 (03/16 01:20)