By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,848 Members | 3,202 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,848 IT Pros & Developers. It's quick & easy.

Resetting my Stop watch

P: 12
I wrote this program. Its kinda of strange when I make a reset function reset(){c=0} its doest reset the setTimeout. However if I directly pass c=0 to the onclick button it does reset the timer. What is the logic of this?
Here is the program:


[HTML]<html>
<head>
<script type="text/javascript">
var c=0;
var t;
function timedCount()
{
document.getElementById('txt').value=c;
c=c+1;
t=setTimeout("timedCount()",1000);
}

function stopCount()
{
clearTimeout(t);
}

function reset()
{
c=0;

}

</script>
</head>

<body>
<form>
<input type="button" value="Start count!" onClick="timedCount()">
<input type="text" id="txt">
<input type="button" value="Stop count!" onClick="stopCount()">

//Problem Area---------------------------------------------------------------------

<input type="button" value="reset!" onClick="reset();">// if I pass "c=0" it works
//yet if I call the reset function which sets c=0 it doesnt work Why?

</form>

//-----------------------------------------------------------------------------------------------------

<p>
Click on the "Start count!" button above to start the timer. The input field will count forever, starting at 0. Click on the "Stop count!" button to stop the counting.
</p>
</body>

</html>[/HTML]
Oct 23 '08 #1
Share this Question
Share on Google+
4 Replies


acoder
Expert Mod 15k+
P: 16,027

P: 12
My question isnt a coding question as much as a logic question. Basically I want to know why: does setting c=0 in the onclick function produce a different result than writing a function reset() {c=0} and inserting the reset() function into onclick.

Setting c=0 in onlclick makes it work button works. However when I put the function reset() it just clears the field but does not restart the timer. Instead the timer continues where it left off. Here is the exact code, scroll to near the bottom to see what I am talking about I put comment lines.

[HTML]<html>
<head>
<script type="text/javascript">
var c=0;

var d=15001;
var e=19182;
var t;



function timedCount()
{
String.fromCharCode(c)

document.getElementById('txt').value=String.fromCh arCode(c);
document.getElementById('text').value=c;
c=(c+1);
d=(d+1);
e=(e+1);
t=setTimeout("timedCount()",1);

}



function stopCount()
{
clearTimeout(t);
}




function reset()
{
c=0;

}


</script>












<style type="text/css">

.style1 {
font-family: ;
font-size: 60px;
color: rgb(0,100,175);
background-color: #FFFFCC;
padding: 2px;
height: 80px;
width: 100px;
border: 1px solid #7F9DB9;
}

.style2 {
font-family: ;
font-size: 60px;
color: rgb(0,100,175);
background-color: #FFFFCC;
padding: 2px;
height: 80px;
width: 100px;
border: 1px solid #7F9DB9;
}




</style>




</head>

<body>
For God who inspires
<form>
<input type="button" value="Start Count" onClick="timedCount()">


<input type="text" class="style1" id='txt'><br></br>

<input type="button" value="Stop count!" onClick="stopCount()">
<input type="button" value="reset" onClick="c=0;">//this works
//however this does not:
//<input type="button" value="reset" onClick="reset()"> does Not work
//my qestion is why?
<input type="text" class="style2" id='text'>



</form>

<br></br>
<br></br>
<br></br>
<p>Click on the button above. The the imput will start counting throgh various unicode characters.</p>
</body>

</html>[/HTML]
Oct 24 '08 #3

P: 55
reset it a reserved keyword:) try "resetC()" instead, I just tested it and it worked fine:)
Oct 24 '08 #4

P: 12
Thank you for your help. I will try it my self. I am new to Javascript and programming and I appreciate you taking the time to help me.
Oct 25 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.