Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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);
    }

  • #2
    Regular Coder
    Join Date
    Mar 2009
    Location
    Portland Oregon
    Posts
    690
    Thanks
    44
    Thanked 63 Times in 62 Posts
    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>


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •