javascript window.clearInterval()和window.clearTimeout无法正常工作 [英] javascript window.clearInterval()and window.clearTimeout not working

查看:153
本文介绍了javascript window.clearInterval()和window.clearTimeout无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在使用javascript的SetInterval()setTimeout()方法清除上述方法,例如,我在按钮中单击我正在使用window.clearInterval()和window.clearTimeout清除它们.但这是行不通的.任何帮助将不胜感激.
这是我的代码:


I am using SetInterval() and setTimeout() methods of javascript to clear the above methods Iam using window.clearInterval()and window.clearTimeout in the button click I am trying to clear them.But it''s not working. any help would be appreciated.
here is my code:

<script type="text/javascript">
       var SD;
       var interval;
       function TimeOut() {
           if (!interval && !SD) {
               interval = setInterval(function() { window.location("TimeUp.aspx") }, 1200000);
               countDown();


           }
       }

       var sec = 0;   // set the seconds
       var min = 02;   // set the minutes

       function countDown() {
           sec--;
           if (sec == -01) {
               sec = 59;
               min = min - 1;
           }
           else {
               min = min;
           }

           if (sec <= 9) { sec = "0" + sec; }

           time = (min <= 9 ? "0" + min : min) + " min and " + sec + " sec ";

           if (document.getElementById) { document.getElementById('theTime').innerHTML = time; }

           SD = window.setTimeout("countDown();", 1000);
           if (min == '00' && sec == '00') { sec = "00"; window.clearTimeout(SD); }
       }
      window.onload = countDown;

       function StopInterval() {
           window.clearInterval(interval);
           window.clearTimeout(SD);
           interval = null;
           SD = null;
           document.getElementById('theTime').innerHTML = "";
           TimeOut();
       }
   </script>





<body onload="TimeOut()">
    <form id="form1" runat="server"></pre>

<table width="100%">
        <tr>
            <td width="100%" align="center">
                <span id="theTime" class="timeClass"></span>
            </td>
        </tr>
    </table></pre>
<asp:Button ID="btnNext" Text="Next" OnClientClick="StopInterval()"

                        runat="server" OnClick="btnNext_Click" Style="height: 26px" Width="82px" />



谢谢
Pradeep Anugu.



Thank you,
Pradeep Anugu.

推荐答案

在此处完全可以正常运行:

Works perfectly fine over here:

<html>
<head>
<title>timer test</title>
<script type="text/javascript">
       var SD;
       var interval;
       function TimeOut() {
           if (!interval && !SD) {
               interval = setInterval(function() { window.location("TimeUp.aspx") }, 1200000);
               countDown();
 

           }
       }
 
       var sec = 0;   // set the seconds
       var min = 02;   // set the minutes

       function countDown() {
           sec--;
           if (sec == -01) {
               sec = 59;
               min = min - 1;
           }
           else {
               min = min;
           }
 
           if (sec <= 9) { sec = "0" + sec; }
 
           time = (min <= 9 ? "0" + min : min) + " min and " + sec + " sec ";
 
           if (document.getElementById) { document.getElementById('theTime').innerHTML = time; }
 
           SD = window.setTimeout("countDown();", 1000);
           if (min == '00' && sec == '00') { sec = "00"; window.clearTimeout(SD); }
       }
      window.onload = countDown;
 
       function StopInterval() {
           window.clearInterval(interval);
           window.clearTimeout(SD);
           interval = null;
           SD = null;
           document.getElementById('theTime').innerHTML = "";
           TimeOut();
       }
   </script>
   </head>
<body onload="TimeOut()">
    <form id="form1" runat="server"></pre>
 
<table width="100%">
        <tr>
            <td width="100%" align="center">
                <span id="theTime" class="timeClass"></span>
            </td>
        </tr>
    </table></pre>
<input type="button" ID="btnNext" Value="Next" onclick="StopInterval()"/>
</body>
</html>



您能否向我们展示服务器端代码的作用?您是否尝试删除了Onclick和Runat属性?



Could you show us what the Serverside Code does? Did you try to remove the Onclick and Runat Attribute?


这篇关于javascript window.clearInterval()和window.clearTimeout无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆