1
我有這樣的代碼
var base_url = 'http://api.zxc.loc';
var questions = [];
function get_question(){
if(!questions.lenght){
$.get(base_url + '/api/questions', function(data){
questions = data;
console.log('get_question', questions); // GOOD, return [Object, Object ... etc ... ]
}, 'json')
}
console.log('get_question_2', questions); // BAD return empty array []
/* var question = questions[0];
delete questions[0];
return question; */
}
怎麼可以看到,在$不用彷徨questions
變量不是空的,但經過$不用彷徨,questions
是空的!
爲什麼?謝謝
簡單的回答你不能這樣做,因爲Ajax請求是異步的 –