Javascript Countdown Timer


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!

DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;

<

html xmlns="http://www.w3.org/1999/xhtml&quot; >

<

head runat="server">

<title>Countdown Timer Demo</title>

<script type="text/javascript">

var hrs;

var mins;

function cd() {

hrs = 1 * h(

"1");

mins = 0 + m(

":30");

redo();

}

function h(obj) {

for(var i = 0; i < obj.length; i++) {

if(obj.substring(i, i + 1) == ":")

break;

}

return(obj.substring(0,i));

}

function m(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(hrs,mins) {

var disp;

if(hrs<= 9) {

disp =

" 0";

}

else {

disp = " ";

}

disp += hrs + ":";

if(mins <= 9) {

disp +=

"0" + mins;

}

else {

disp += mins;

}

return(disp);

}

function redo() {

mins–;

if(mins == -1) {

mins= 1;

hrs–;

}

document.cd.disp.value = dis(hrs,mins);

if((hrs == 0) && (mins == 0)) {

window.alert(

"Time is up. Press OK to continue.");

}

else {

cd = setTimeout("redo()",60000);

}

}

function init() {

cd();

}

window.onload = init;

</script>

</head>

<

body>

<form id="cd" runat="server">

<div>

<input id="txt" type="text" value="10:00" name="disp" />

</div>

</form>

</

body>

</

html>

About ajaybhingare

Consultant and Corporate Trainer for MS .NET Technology
This entry was posted in Computers and Internet. Bookmark the permalink.

Leave a comment