Ajax的同步與異步


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="AjaxByJquery.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script src="jquery-1.8.3.min.js" type="text/javascript"></script>
   
   <script type="text/javascript">
       // 當把asyn設為false時,這時ajax的請求時同步的,也就是說,這個時候ajax塊發出請求后,他會等待在function1()這個地方,不會去執行function2(),直到function1()部分執行完畢。
       // 當把asyn設為true時,這時ajax的請求時異步的,當ajax塊發出請求后,他將停留function1(),等待server端的返回,但同時(在這個等待過程中),前台會去執行function2(),
       $(document).ready(function () {
           $.ajax({
               type: "POST",
               url: "value.aspx?act=init",
               dataType: "html",
               async:true,           
               success: function (result) {   function1()  
                  
               }
           });
           function2();
        }

   );
       function function1() {
           alert('function1');
       }
       function function2() {
           alert('function2');
       }


   </script>
</head>
<body>
    <form id="form1" runat="server">
     <div>
    
    </div>
    </form>
</body>
</html>

參考http://blog.csdn.net/cursor2000/article/details/5677479
代碼下載


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM