
Some JavaScripting today! We are going to create a simple countdown timer using JavaScript. What’s the use? Umm, I really am not creative enough to find any of its perfect use but it could be used somewhere, sometimes…and there is no harm in learning something even when there seems to be no potential use of it. Who knows maybe you’d need it sometime to add creativity to your web pages. Of course some of the techniques that we are going to use will be needed at many times, so you won’t wanna miss this!
This time , let’s start off with the code first:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>JavaScript Countdown Timer</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script language="JavaScript" type="text/JavaScript">
//to store timeout ID
var tID;
function tickTimer(t)
{
//if time is in range
if(t>=0)
{
document.writeln(t);
t=t-1;
tID=setTimeout("tickTimer('"+t+"')",1000);
}
//stop the timeout event
else
{
killTimer(tID);
document.writeln("<br /> <font color='#ff0000'>Time Out!</font>");
}
}
//function to stop the timeout event
function killTimer(id)
{
clearTimeout(id);
}
</script>
</head>
<body onLoad="tickTimer(10)" onUnload="killTimer(tID)">
</body>
</html>
Now let’s analyze the code:
1. We have created two functions tickTimer()
and killTimer()
2. We have defined two event handlers onLoad
and onUnload which’d call the respective functions at respective
events.
When the code above (as a web page) is executed, it’d proceed as:
1. First the onLoad event calls tickTimer
function with the initial time, the countdown timer has to be ticked down form.
2. The function displays the initial time remaining,
does some calculations and calls a method setTimeout().
3. The setTimeout function now calls the
function passed, every 1000 milliseconds 1 second). On setting the timeout event
this method returns a unique ID which would be used to stop the timeout event
when needed (onUnload or when timer has ticked down to 0).
One thing you may get confused with is how without loop or anything as such,
are we able to count the timer down. Answer is, because JavaScript is an event
driven language. First, we are defining a body onLoad event to
make a call to some function as the web page is loaded. Second, we are defining
a timeout event that would call the function itself (recursive call) every one
second indefinitely until the timeout event is cleared. We are clearing the
timeout either when the countdown timer reaches 0 or when the page gets unloaded
(onUnload).
Had JavaScript not been an event driven language, we’d need to have a loop to check when a second has elapsed and update the variable accordingly. Luckily we don’t have to!
But as it is, can we embed or place the above timer in a web page the way we want, styled and perfectly aligned. Or how about a single number getting counted down rather than showing all the numbers as the countdown proceeds. We’ll see that in the next post!
Previous Posts:
HI,
ReplyDeleteI wanna add a GIF file into my blog post. I added a GIF file but it can not display as a GIF format. It simply shown like a ordinary image.
I saw your "js_count_down_timer01" GIF file, i wanna show my image also as yours. Give ur suggestion or write in New Post.
Do you mean to say animated GIF file. Does it look static when added. You might need to change the setting such as "loop forever" and "timings" for each frame when creating the animated GIFs.
ReplyDeleteDo ask again if in doubt!
-Arvind
This comment has been removed by a blog administrator.
ReplyDeletevery cool & good tip, thank you very much for sharing.
ReplyDeleteAnd alternate from my site yoou can use JavaScript Countdown Timer
One thing you may get confused with is how without loop or anything as such, are we able to count the timer down
ReplyDelete