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