Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-30-2009, 05:24 AM   PM User | #1
mwebster
New to the CF scene

 
Join Date: Mar 2009
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
mwebster is an unknown quantity at this point
Program Not Working!

Hey guys, I'm new to JavaScript and HTML and I am trying to make a program that works as a stopwatch. I don't think the program is even incorporating the .js file at all, but it's in the right directory and included as the correct name, so I'm not sure. It probably is being incorporated, but none of the functions are working. Here's my code:

homework06.html
Code:
<!--3.27.09-->
<!--Homework 6-->
<!--Create a stop watch-->
<html>

	<head>
		<title>Stop Watch</title>
		<script language="JavaScript" src="homework06.js"></script>
		<noscript><p>Your browser is not configured to run scripts</p></noscript>
		<style type="text/css">
			#time {text-align: center; font-size: 30px}
			#inputs {text-align: center}
		</style>
	</head>

	<body>
		<fieldset>
				<form name="stopwatch" id="stopwatch" action="" method="get">
						<p id="time">00:00:00</p>
				</form>
		</fieldset>
		<p id="inputs">
			<input type="button" value="Start" onclick="start()" />
			<input type="button" value="Stop" onclick="stop()" />
			<input type="button" value="Clear" onclick="empty()" />
		</p>
	</body
</html>
homework06.js
Code:
var hrs=0;
var mins=0;
var secs=0;
var interval;

function increment(){
	alert("incerment");
	if(mins<59&&secs<59){
		secs++:
	}else if(secs==59){
		if(mins==59){
			hrs++;
			mins=0;
			secs=0;
		}else{
			mins++;
			secs=0;
		}
	}
	print();
}

function print(){
	alert("print");
	alert(hrs+":"+mins+":"+secs);
	document.getElementById('time').innerHTML = hrs + ':' + mins + ':' +secs;
}

function empty(){
	alert("clear");
	hrs=0;
	mins=0;
	secs=0;
}


function stop(){
	alert("stop");
	clearInterval(interval);
}


function start(){
	alert("start");
	interval=setInterval("increment()", 1000);
}
mwebster is offline   Reply With Quote
Old 03-30-2009, 06:10 AM   PM User | #2
TinyScript
Regular Coder

 
Join Date: Mar 2009
Location: Portland Oregon
Posts: 690
Thanks: 44
Thanked 63 Times in 62 Posts
TinyScript is on a distinguished road
You are missing some braces in your if/else and
secs++: should be secs++;
Turn off the alerts too or you get stuck in an endless loop of alerts

Code:
Try to fix it your self first. This works below, but you won't learn if you don't do it yourself.
<!--3.27.09-->
<!--Homework 6-->
<!--Create a stop watch-->
<html>

	<head>
		<title>Stop Watch</title>
		<script >
var hrs=0;
var mins=0;
var secs=0;
var interval;

function increment(){
	
	if(mins<59&&secs<59){
		secs++;
	}
else {
	if(secs==59){
		if(mins==59){
			hrs++;
			mins=0;
			secs=0;
				}
}else{
			mins++;
			secs=0;
		}
	}
	print();
}

function print(){
	
	//alert(hrs+":"+mins+":"+secs);
	document.getElementById('time').innerHTML = hrs + ':' + mins + ':' +secs;
}

function empty(){
	//alert("clear");
	hrs=0;
	mins=0;
	secs=0;
}


function stop(){
	//alert("stop");
	clearInterval(interval);
}


function start(){
	//alert('start');
	setInterval("increment()", 1000);
}
</script>
		<noscript><p>Your browser is not configured to run scripts</p></noscript>
		<style type="text/css">
			#time {text-align: center; font-size: 30px}
			#inputs {text-align: center}
		</style>
	</head>

	<body>
		<fieldset>
				<form name="stopwatch" id="stopwatch" action="" method="get">
						<p id="time">00:00:00</p>
				</form>
		</fieldset>
		<p id="inputs">
			<input type="button" value="Start" onclick="start()" />
			<input type="button" value="Stop" onclick="stop()" />
			<input type="button" value="Clear" onclick="empty()" />
		</p>
	</body
</html>
TinyScript is offline   Reply With Quote
Reply

Bookmarks

Tags
beginner, html, javascript, newb

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:29 PM.


Advertisement
Log in to turn off these ads.