2011-06-03 251 views
2

看來我的腳本不想等待$ .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> 

是什麼,以確保我的電話後繼續完成之前,不掛在瀏覽器的最佳方法是什麼?希望有一些不太混亂的東西。 :)

+0

檢查括號 – zod 2011-06-03 18:11:07

+3

提供回調函數。 Ajax是**異步**,瀏覽器不會等到收到響應! – 2011-06-03 18:12:20

+0

@zod - 在代碼的其餘部分添加括號會使其變得非常難看,但這是唯一的方法嗎? – Jeff 2011-06-03 18:13:39

回答

4

不是太亂了正確使用回調。

只需在.post()呼叫之外創建一些功能,並在您認爲合適的時候在.post()之內調用它。您可以製作很多回調,並以非常靈活的方式在AJAX調用中使用它們。

在您的情況下,因爲您只撥打alert(),因此無需創建附加功能 - 只需致電.post()撥打alert()即可。如果您的代碼變大,請考慮創建單獨的函數。

這就是JavaScript和異步調用的工作原理。習慣它,並用它的力量編寫非常乾淨和可維護的代碼。

+0

)中,因此在代碼的頂部定義一個函數,並將其用作回調函數而不是匿名函數? – Jeff 2011-06-03 18:18:57

+0

@Jeff其實你可以做到這一點,但我建議從匿名函數內部調用它,這意味着你可以在這個匿名函數中檢查一個條件並調用MyFunction1或MyFunction2,這取決於結果是什麼這是否足夠清楚,或者你需要一些例子如何做到這一點? – Tadeck 2011-06-03 18:21:44

+0

我相信我明白了:) – Jeff 2011-06-03 18:24:06

2
<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; 
    // it reaches this, showing us that there was no error! 
    alert('Im alive!!!'); 
    // and a whoooole bunch of other code here... 

    } 
    else 
    { 
    test = false; 
    // THIS gets executed, despite the fact that we set test to true! 
    alert('Awww....'); 

    } 
    } 
} 
</script> 
+0

這是更接近,但不是正確的。你在最後兩行缺少結束符。 – zsalzbank 2011-06-03 18:13:40

+0

將它包裝在一個匿名函數中會更好,保持範圍更好和更乾淨。考慮到大多數情況下你把代碼放在一個'$(document).ready(function(){' – PeeHaa 2011-06-03 18:15:03

0

太亂了?如果您縮進多個空格,則所有內容都更具可讀性,並且仍然可以正常工作。

var test = false; // NOW it's global 

// Just so we can use the method again 
function postSomething() { 
    $.post('test.php', {}, function(result) { 
    if(result === 'yes') { 
     alert('Its true! Hurraaay!'); 
     test = true; 
     alert('Im alive!!!'); 
    } else { 
     test = false; 
     alert('Awww....'); 
    } 
    }); 
} 

$(document).ready(function() { 
    postSomething(); 
}); 

儘管如此,

0

JQuery .post()方法異步連接到您的服務器腳本。您可以利用回調功能,以便程序在腳本返回響應後使用數據。

而不是試圖在發送調用之後同步處理數據,而是在收到響應時處理數據。利用函數使代碼更具可讀性。

$(function() { 
    postTest(); 
}); 

function postTest() { 
    $.post(
     'test.php', 
     {}, 
     function(response) { 
      if(response == 'yes') { 
       testSuccess(); 
      } else { 
       testFailure(); 
      } 
     } 
    ); 
} 

function testSuccess() { 
    alert("Success"); 
} 

function testFailure() { 
    alert("Failure"); 
}