Click here to Skip to main content
15,921,990 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I've created a countdown timer using javascript in asp.net. After completion of time, Button1 becomes disabled, but when I reload the page, the countdown timer is reset and Button1 is enabled.

I want to permanently disable Button1 when timer is equal to zero. My code is:


What I have tried:

JavaScript
var tim;
var min = 01;
var sec = 00;
var f = new Date();
function f1() {
  f2();
}

function f2() {
  if (parseInt(sec) > 0) {
    sec = parseInt(sec) - 1;
    document.getElementById("showtime").innerHTML = ""+min+" Minutes ,"+sec+" Seconds";
    tim = setTimeout("f2()", 1000);
  }
  else {
    if (parseInt(sec) == 0) {
      min = parseInt(min) - 1;
      if (parseInt(min) == -1) {
        clearTimeout(tim);
        $("#Button1").prop('disabled', true);
      }
      else {
        sec = 60;
        document.getElementById("showtime").innerHTML = "" + min + " Minutes ," + sec + " Seconds";
        tim = setTimeout("f2()", 1000);
      }
    }
  }
}



<body onload="f1()">
<div><h3>Time will be finished after:</h3>
</div>
<div id="showtime"></div>
<div> <asp:Button ID="Button1" runat="server" Text="Submit"/></div>`
</body>
Posted
Updated 15-Aug-17 21:08pm

1 solution

Example for you
-----------------------
<body  onload="display_c(1121);" >

<script type="text/javascript">

    function display_c(start) {
        window.start = parseFloat(start);
        var end = 0 // change this to stop the counter at a higher value
        var refresh = 1000; // Refresh rate in milli seconds
        if (window.start >= end) {
            mytime = setTimeout('display_ct()', refresh)
        }
        else {
            display_ct()();
             $("#Button1").prop('disabled', true);
            alert("Time Over ");
   
        }
    }
    function display_ct() {
        // Calculate the number of days left
       
        var days = Math.floor(window.start / 86400);
        // After deducting the days calculate the number of hours left
        var hours = Math.floor((window.start - (days * 86400)) / 3600)
        // After days and hours , how many minutes are left 
        var minutes = Math.floor((window.start - (days * 86400) - (hours * 3600)) / 60)
        // Finally how many seconds left after removing days, hours and minutes. 
        var secs = Math.floor((window.start - (days * 86400) - (hours * 3600) - (minutes * 60)))

        var x = window.start + "(" + days + " Days " + hours + " Hours " + minutes + " Minutes and " + secs + " Secondes " + ")";
        document.getElementById('day').innerHTML = days;
        document.getElementById('hours').innerHTML = hours;
        document.getElementById('minutes').innerHTML = minutes;
        document.getElementById('second').innerHTML = secs;
     window.start = window.start - 1;

        tt = display_c(window.start);

    }
   </script>
 
Share this answer
 
Comments
OwaisCh 17-Aug-17 5:29am    
Didn't work ;(
Richard Deeming 17-Aug-17 11:44am    
If it didn't work, why have you accepted the solution?
OwaisCh 17-Aug-17 13:20pm    
I'm new to this website. I don't know much about its rules and regulations. 😞
Richard Deeming 17-Aug-17 14:58pm    
Code Project Quick Answers FAQ | How does accepting solutions work?[^]

That point possibly needs clarification: you should only accept a solution if it solves your question. If it doesn't work, or it doesn't answer the question you asked, don't mark it as "accepted".

There should be a button on the solution which will let you un-accept it.
vermanishad 18-Aug-17 2:26am    
    <script src="http://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

</head>
  <script type="text/javascript">

      function display_c(start) {
          window.start = parseFloat(start);
          var end = 0 // change this to stop the counter at a higher value
          var refresh = 1000; // Refresh rate in milli seconds
          if (window.start >= end) {
              mytime = setTimeout('display_ct()', refresh)
          }
          else {
              alert("Time Over ");
             
          
          }
      }
      function display_ct() {
          // Calculate the number of days left
        
          var days = Math.floor(window.start / 86400);
          // After deducting the days calculate the number of hours left
          var hours = Math.floor((window.start - (days * 86400)) / 3600)
          // After days and hours , how many minutes are left 
          var minutes = Math.floor((window.start - (days * 86400) - (hours * 3600)) / 60)
          // Finally how many seconds left after removing days, hours and minutes. 
          var secs = Math.floor((window.start - (days * 86400) - (hours * 3600) - (minutes * 60)))

          var x = window.start + "(" + days + " Days " + hours + " Hours " + minutes + " Minutes and " + secs + " Secondes " + ")";
          document.getElementById('day').innerHTML = days;
          document.getElementById('hours').innerHTML = hours;
          document.getElementById('minutes').innerHTML = minutes;
          document.getElementById('second').innerHTML = secs;
        
         window.start = window.start - 1;

          tt = display_c(window.start);
          
      }
   </script>

<body onload="display_c(12);">
    <form id="form1" runat="server">
    <div>
        <span id="day"></span><span id="hours"></span><span id="minutes"></span><span id="second"></span>
    <input type="button" id="Button1" value="hii" />
    </div>
    </form>

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