【问题标题】:Change body onLoad on img click在 img 点击时更改正文 onLoad
【发布时间】:2012-06-09 06:49:45
【问题描述】:

我有一个带有标签 <body onload="setInterval('window.location.reload()', 60000);"> 的页面,该标签会导致页面每 1 分钟刷新一次。

我想制作一个按钮 ( PAUSE ),单击该按钮会取消刷新。

我尝试了以下方法,但它不起作用:

window.onload= function () {};

【问题讨论】:

    标签: javascript asp.net javascript-events


    【解决方案1】:

    使用clearInterval()

    在您的脚本中:

    var pid=false;
    function clear()
    {
       clearInterval(pid);
       pid=false;
    }
    function resume()
    {
       if(!pid)
         pid=setInterval('window.location.reload()', 60000); 
    }
    

    在体内

    <body onload="pid=setInterval('window.location.reload()', 60000);">....
    <button onclick="clear()">Pause</button>
    <button onclick="resume()">Resume</button>....
    

    【讨论】:

      【解决方案2】:

      最好将脚本放在一个函数中,这样你也可以从其他地方调用它。

      像这样……

      <html>
      <head>
          <script type="text/javascript">
              var intervalRef;   
              function ResumeRefresh(interval){
                  intervalRef = setInterval('window.location.reload()', interval);
              }
              function StopRefresh(){
                  intervalRef=window.clearInterval(intervalRef);
              }
          </script>
      </head>
      <body onload="ResumeRefresh(60000);">
          <input type="button" val="Stop Refreshing" onclick="StopRefresh();"></input>
      </body>
      

      【讨论】:

        【解决方案3】:

        你可以像这样拨打clearInterval()

        id = setInterval('func', time);
        <button onlcick="clearInterval(id)">Pause</button>
        

        clearInterval()window 对象中的一个方法,用于清除您的setInterval()

        Here's a link 了解更多信息

        【讨论】:

          【解决方案4】:

          你需要这样的东西:

          <script>
          var reloadInterval;
          var run = true;
          function setMyInterval()
          {
             reloadInterval = setInterval('window.location.reload()', 60000);
          }
          function Pause()
          {
             if(run)
             {
                clearInterval(reloadInterval);
             }
             else 
             {
                reloadInterval = setInterval('window.location.reload()', 60000);
             }
          }
          </script>
          

          html:

          <body onload = 'setMyInterval()'>
          <button onlcick="Pause()">Pause / Play</button>
          

          【讨论】:

            猜你喜欢
            • 2012-09-28
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-06-27
            • 2010-09-16
            相关资源
            最近更新 更多