js + ajax實現自動刷新界面或局部刷新

最近在作一個網站,須要用到實時刷新,因而就學習了一下有關資料,跟你們share一下。javascript

 

1)其實刷新不用js也能夠實現。單純的html能夠用meta標籤搞定html

 

  1. <meta http-equiv="refresh"content="10;url=xxx.com">  
  2. //表示間隔10秒後刷新,即打開網頁後10秒就跳轉到了xxx.com中  

2)js的話,有一個reload的函數和navigate的函數能夠實現刷新整個頁面java

  1. <script language=''javascript''>  
  2. window.location.reload(true);  
  3. </script>  
  4. //若是是你要刷新某一個iframe就把window給換成frame的名字或ID號  
  5. //navigate函數是個跳轉函數,若是你給函數傳參爲自身的url,等於從本頁面跳到了本頁面,也實現了刷新的效果。  
  6. <script language=''javascript''>  
  7. window.navigate(」localUrl");  
  8. </script>  

3)實現定時刷新ajax

 

  1. <script>setTimeout("location.href='url'",2000)</script>  
  2. //url是要刷新的頁面URL地址  
  3. //2000是等待時間=2秒,  

4)jsp中定時刷新jsp

 

  1. <% response.setHeader("refresh","1"); %>  
  2. //每一秒刷新一次  

 

5)js+ajax局部刷新:ide

 

  1. setTimeout(function() {  
  2.              Push();  
  3.            },  
  4.         200);//setTimeout 程序執行後200毫秒執行push,可是僅此一次。execute the function push only one time after the program began to work  
  5.         setInterval(function() {  
  6.   
  7.             Push();  
  8.   
  9.     },  
  10.         3000);//三秒後執行 execute it after 3 seconds  
  11.   
  12. /*請求函數的ajax*/  
  13.   
  14. function Push() {  
  15.   
  16.   
  17.     $.ajax({  
  18.   
  19.         type: "POST",  
  20.         url: "url.jsp",//須要跳轉到的界面 the page you want to post data  
  21.         data: {  
  22.             data: data//要傳給後臺的數據 the data you should send to background  
  23.         },  
  24.         beforeSend: function() {},//在發送以前你能夠進行相關操做 what you want to do before send  
  25.         success: function(data) {  
  26.             //須要執行的操做 execute it  
  27.            }else {  
  28.         $(".tongzhi").html(0).hide();  
  29.         }  
  30.     }  
  31.     });  
相關文章
相關標籤/搜索