Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi,
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:
JavaScript
<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>


ASP.NET
<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" />


Thank you,
Pradeep Anugu.
Posted
v3
Comments
Sergey Alexandrovich Kryukov 1-Aug-12 12:12pm    
"Not working" is not descriptive.
--SA
Ganesan Senthilvel 1-Aug-12 15:02pm    
Share the error details

1 solution

Works perfectly fine over here:

HTML
<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>


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

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900