AF
HomeTagSubmit NotesAsk AnythingLoginSubscribe Us
AF
1. Feel Free to ask and submit anything on Anyforum.in and get satisfactory answer
2. Registration is not compulsory, you can directly login via google or facebook
3. Our Experts are looking for yours ?.



javascript-timer: How to display countdown timer by javascript ?

I want to display a countdown timer on a webpage in the following format :
Min : Sec
and when it´s 0:0 an alert box should be displayed. Please give me source code.

javascript x 33
timer x 1
Posted On : 2013-12-03 20:35:42.0
profile Garima Gupta - anyforum.in Garima Gupta
596129560202
up-rate
5
down-rate

Answers


Nice Question, Following code is working javascript code for countdown timer. In this we enter the minute and second in the text field and click on start button. Countdown timer will be displayed under the div tag having it´s id "hi" . and you can change it as per your wish. Whatever style you want to apply on countdown timer define it for id="hi".

************************* Example of Countdown timer by Javascript *************************

countdown-timer.html :
------------------------------

<html>
<head>
<script>
var mins
var secs;

function cd() {
var min=document.getElementById("min").value;
var sec= document.getElementById("sec").value;
sec++;
   mins = 1 * m(min); // change minutes here
   secs = 0 + s(":"+sec); // change seconds here (always add an additional second to your total)
   redo();
}

function m(obj) {
   for(var i = 0; i < obj.length; i++) {
         if(obj.substring(i, i + 1) == ":")
         break;
   }
   return(obj.substring(0, i));
}

function s(obj) {
   for(var i = 0; i < obj.length; i++) {
         if(obj.substring(i, i + 1) == ":")
         break;
   }
   return(obj.substring(i + 1, obj.length));
}

function dis(mins,secs) {
   var disp;
   if(mins <= 9) {
         disp = " 0";
   } else {
         disp = " ";
   }
   disp += mins + ":";
   if(secs <= 9) {
         disp += "0" + secs;
   } else {
         disp += secs;
   }
   return(disp);
}

function redo() {
   secs--;
   if(secs == -1) {
         secs = 59;
         mins--;
   }
   document.getElementById("hi").innerHTML = dis(mins,secs);// setup additional displays here.
   if((mins == 0) && (secs == 0)) {
     alert("Time is over");
   }
    else {
       cd = setTimeout("redo()",1000);
   }
}

function init() {
 cd();
}
</script>
</head>
<body>
<div id="hi" style="color:red;font-size:24px;"></div>
Minute : <input type="text" name="min" id="min"/><br/>
Second : <input type="text" name="sec" id="sec"/><br/>
<input type="button" value="start" onclick="init()" />

</body>
</html>

******************************* Output of the above html code ******************************

Minute :
Second :

Posted On : 2013-12-03 20:49:51
Satisfied : 1 Yes  0 No
profile Rishi Kumar - anyforum.in Rishi Kumar
523188250048
Reply This Thread
up-rate
4
down-rate



Post Answer
Please Login First to Post Answer: Login login with facebook - anyforum.in