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.     });  
相关文章
相关标签/搜索