473,385 Members | 1,597 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Resetting my Stop watch

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
4 2077
acoder
16,027 Expert Mod 8TB
See this similar recent thread.
Oct 24 '08 #2
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
reset it a reserved keyword:) try "resetC()" instead, I just tested it and it worked fine:)
Oct 24 '08 #4
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

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

Similar topics

8
by: riprod | last post by:
Someone in the IIS newsgroup suggest I post this here, so sorry in advance for the cross posting. I have a Win 2003 SP1 with IIS 6 and host about 40 websites, most of them useing ASP/VB with...
4
by: Richard Haygreen | last post by:
I have an HTML form which includes a combo box with 4 options, which is used to select a method of contact (phone, email etc...). If the user selects an invalid option (for example, they have...
2
by: Job Lot | last post by:
how can i create a stop watch using vb.net i am performing a data extraction job using windows forms and want to display time elapsed on the form. how can i do it? Thanks
8
by: Matt Theule | last post by:
While stepping through an ASP.NET project, I found that data was being inserted into my database even though I was not stepping through the code that inserted the data. I have a single page with...
8
by: Rob Bazinet | last post by:
Hello all; I have an ASP.NET application which I run part of it in a new thread. The part running is very processor and time intensive and takes an hour plus to complete. The problem I am...
4
by: Ian Davies | last post by:
Hello I am struggling for a solution to clear some fields on my webpage that takes their values from some sessions My solution below works when the button is clicked twice. I sort of know why I...
2
by: tshad | last post by:
I have my Application variables getting set in my Global.aspx file. If I change a variable in my table that is read at startup, I need to restart the application to reload the variables. I...
3
by: GiJeet | last post by:
I put the the button property in the watch window but it doesn't stop when the property changes value. For example I put this.myButton.Visible in the watch window with an initial value of false...
11
by: uzairmemon | last post by:
Any one Have coding of Reverse stop watch?
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.