在图像上单击更改正文onLoad

时间:2012-06-09 06:49:46

标签: javascript asp.net javascript-events

我的网页标记为<body onload="setInterval('window.location.reload()', 60000);">,导致网页每1分钟刷新一次。

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

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

window.onload= function () {};

4 个答案:

答案 0 :(得分:3)

使用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>....

答案 1 :(得分:1)

您可以像这样致电clearInterval()

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

clearInterval()window对象中清除setInterval()

的方法

Here's a link了解更多信息

答案 2 :(得分:1)

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

喜欢这个......

<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 :(得分:1)

你需要这样的东西:

<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>
相关问题