我有一個全球性的array
我想console.log(array_name)
,但我得到了一個未定義的錯誤 下面是我的代碼:JavaScript的全局數組不能登錄
<script type="text/javascript">
var profit = [];
$(document).ready(function(e) {
$.ajax({
url : "/php/get-inflow.php",
dataType: "json",
type: "POST",
success: function(data){
for(var i =0; i<data.length; i++){
if(data[i] == null){
profit[i] = 0; // logging profit[i] here gives me correct value
}else{
profit[i] = parseInt(data[i]); // logging profit[i] here gives me correct value
}
}
}
});
console.log(profit);
//some other functions.......
});
</script>
當我看到控制檯我得到的輸出[ ]
這意味着一個空白陣列......
是否將利潤數組正確設置爲全局(新的jquery) 如何訪問全局數組和其他函數 謝謝!
ajax是異步的,這是你的'問題'在這裏 –