网页数据实时更新的三种简单方法

一、页面自动刷新
<meta http-equiv="refresh" content="20">
其中20指每隔20秒刷新一次页面.

二、页面自动跳转:
<meta http-equiv="refresh" content="20;url=http://www.hackhome.com">
其中20指隔20秒后跳转到http://www.hackhome.com页面

三、js控制
<script language="JavaScript">
  function myrefresh(){
    window.location.reload();
  }
  setTimeout('myrefresh()',1000); //指定1秒刷新一次
</script>

四、Ajax
var isLoaded = false;
function reqs(opts) {
  $.ajax({
    type: 'get',
    url: 'demo.php',
    dataType: 'json',
    beforeSend: function() {
      if(opts.init === 1) {
      $('.zh-loading').show();
      }
      isLoaded = false;
    },
    success: function(res) {
      console.log(res);
    },
    complete: function() {
      if(opts.init === 1) {
        $('.zh-loading').hide();
      }
    isLoaded = true;
    },
    error: function() {
      console.log('请求失败~');
    }
  });

}

reqs({"init": 1});

setInterval(function() {

  isLoaded && req({"init": 0});

}, 3000);

注释:
1.isLoaded && req({"init": 0}); 表示前面一个条件正确,则执行&&后面的方法。
2.将init赋值为0,是让loading只出现在页面第一次加载的时候。后面不出现。

posted @ 2023-01-30 20:20  KeepDoIT  阅读(2092)  评论(0编辑  收藏  举报