【问题标题】:How to refresh a HTML page once, automatically [duplicate]如何自动刷新HTML页面一次[重复]
【发布时间】:2014-05-23 08:57:29
【问题描述】:

此脚本每 5 秒手动和自动刷新页面。如何让页面在 5 秒后自动重新加载一次。

<!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>
        <title>Refresh or Reload a Page Using JQuery</title>
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
    </head>
    <body>
        <div><input id="btReload" type="button" value="Reload Page" /></div>
    </body>
    <script type="text/javascript">
        $(document).ready(function() {
            $('#btReload').click(function() { location.reload(true); });    // RELOAD PAGE ON BUTTON CLICK EVENT.

            // SET AUTOMATIC PAGE RELOAD TIME TO 5000 MILISECONDS (5 SECONDS).
            setInterval('refreshPage()', 5000);
        });
        function refreshPage() { location.reload(); }
    </script>
    </html>

【问题讨论】:

    标签: javascript jquery html


    【解决方案1】:

    用setTimeout代替setInterval,语法相同

        <script type="text/javascript">
        $(document).ready(function() {
            $('#btReload').click(function() { location.reload(true); });   
    
            // SET AUTOMATIC PAGE RELOAD TIME TO 5000 MILISECONDS (5 SECONDS).
            setTimeout('refreshPage()', 5000);
        });
        function refreshPage() { location.reload(); }
    

    【讨论】:

      【解决方案2】:

      进行如下修改:

      var timerId = setInterval('refreshPage()', 5000);
      

      并更改刷新页面功能。

      function refreshPage() { clearInterval(timerId); location.reload(); }
      

      【讨论】:

      • 谢谢大家。你们太棒了
      【解决方案3】:
      $(document).ready(function () {
      $('#btReload').click(function () { location.reload(true); });    // Manual
      
      TimeSet(); // Auto Once});
      
      function TimeSet() {
      setTimeout(function () {
          // Do something after 5 seconds
          Refresh()
      }, 5000);}
      
      function Refresh() {
      location.reload();}
      

      【讨论】:

        猜你喜欢
        • 2013-03-22
        • 2012-05-23
        • 1970-01-01
        • 2016-09-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-09-05
        • 2013-02-23
        相关资源
        最近更新 更多