Javascript Count down help

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
Cdaman
Posts: 15
Joined: Tue Dec 06, 2011 2:08 am

Javascript Count down help

Post by Cdaman »

How do I make a javascript countdown (I want it to start at 2 minutes and count down to 0 seconds before running some code) I have looked on the web and all that I found is how to make so that it shows a countdown to do with the date (Days, Months, Years until something).
User avatar
Ark
Posts: 427
Joined: Wed Jun 01, 2011 10:25 pm

Re: Javascript Count down help

Post by Ark »

Yes there're awful js countdowns on the web that's why ive made my own.

countdown.html

Code: Select all

<html>
<head>
<script type="text/javascript">
function countdown(countdownId)
{
	if(mins == 0)
	{
		if (hours==0)
		{
			hours = 0;
			
			hours2=(hours<10?"0"+hours:""+hours);
			mins2=(mins<10?"0"+mins:""+mins); 
			sec2=(sec<10?"0"+sec:""+sec);
			
			countdownId.innerHTML=hours2+":"+mins2+":"+sec2;
			return;
		}
		else if(sec==0)
		{
			hours -= 1;
			mins = 59;
			sec = 59
			
			hours2=(hours<10?"0"+hours:""+hours);
			mins2=(mins<10?"0"+mins:""+mins); 
			sec2=(sec<10?"0"+sec:""+sec);
			
			countdownId.innerHTML=hours2+":"+mins2+":"+sec2;
			return;
		}
		else
		{
			sec-=1;
			hours2=(hours<10?"0"+hours:""+hours);
			mins2=(mins<10?"0"+mins:""+mins); 
			sec2=(sec<10?"0"+sec:""+sec);
			
			countdownId.innerHTML=hours2+":"+mins2+":"+sec2;
			return;
		}
		
		
	}
	if (hours == 0 && mins == 0 && sec == 0)
	{
		clearInterval(countdownVar); // ENDS THE COUNTDOWN.	
	}
	else if(sec==0)
	{
		mins -= 1;
		sec = 59;
		
		hours2=(hours<10?"0"+hours:""+hours);
		mins2=(mins<10?"0"+mins:""+mins); 
		sec2=(sec<10?"0"+sec:""+sec);
		
		countdownId.innerHTML=hours2+":"+mins2+":"+sec2;
	}
	else
	{
		sec -=1;
		
		hours2=(hours<10?"0"+hours:""+hours);
		mins2=(mins<10?"0"+mins:""+mins); 
		sec2=(sec<10?"0"+sec:""+sec);
		
		countdownId.innerHTML=hours2+":"+mins2+":"+sec2;
	}
}		<!-- ENDS -->

</script>
</head>

<body>
<p id="countdown">blabla</p>
<script type="text/javascript">
var hours=1;
var mins=0;
var sec=5;

var countId=document.getElementById("countdown");

		hours2=(hours<10?"0"+hours:""+hours);
		mins2=(mins<10?"0"+mins:""+mins); 
		sec2=(sec<10?"0"+sec:""+sec);
		
		countId.innerHTML=hours2+":"+mins2+":"+sec2;

var countdownVar=setInterval("countdown(countId)",1000);

</script>
</body>
</html>
run the script and there your'll see it working.
:) bye!
Orgullo Catracho
User avatar
hallsofvallhalla
Site Admin
Posts: 12026
Joined: Wed Apr 22, 2009 11:29 pm

Re: Javascript Count down help

Post by hallsofvallhalla »

Urban Realms videos has a count down timer.
Cdaman
Posts: 15
Joined: Tue Dec 06, 2011 2:08 am

Re: Javascript Count down help

Post by Cdaman »

thank you, i will try that.
The only videos I have seen are the ones for the browser based mmo. I can't download any others. :(
Post Reply

Return to “Beginner Help and Support”