看來我的腳本不想等待$ .post調用完成。這是一個問題。下面是一些僞代碼:
<script type="text/javascript" language="javascript">
$(document).ready(function(){
// Global var, to be used everywhere in the ready scope
// Note its default value!
var test = false;
$.post('test.php',{},function(result){
if(result=='yes')
{
// This gets executed!
alert('Its true! Hurraaay!');
test = true;
}
else
{
test = false;
}
}
if(test==false)
{
// THIS gets executed, despite the fact that we set test to true!
alert('Awww....');
}
// it reaches this, showing us that there was no error!
alert('Im alive!!!');
// and a whoooole bunch of other code here...
}
</script>
是什麼,以確保我的電話後繼續完成之前,不掛在瀏覽器的最佳方法是什麼?希望有一些不太混亂的東西。 :)
檢查括號 – zod 2011-06-03 18:11:07
提供回調函數。 Ajax是**異步**,瀏覽器不會等到收到響應! – 2011-06-03 18:12:20
@zod - 在代碼的其餘部分添加括號會使其變得非常難看,但這是唯一的方法嗎? – Jeff 2011-06-03 18:13:39