I am working with Javascript StopWatch in C# web Aplication. But I don’t get output from javascript. Below is my code.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script type="text/javascript" language="JavaScript">
var state = 0;
function startstop() {
if (state == 0) {
state = 1;
then = new Date();
then.setTime(then.getTime() - ms);
} else {
state = 0;
now = new Date();
ms = now.getTime() - then.getTime();
document.stpw.time.value = ms;
}
}
function swreset() {
state = 0;
ms = 0;
document.stpw.time.value = ms;
}
function display() {
setTimeout("display();", 50);
if (state == 1) {now = new Date();
ms = now.getTime() - then.getTime();
document.stpw.time.value = ms;
}
}
</script>
</head>
<body onload="display()">
<form id="form1" runat="server">
<div>
Time:
<input type="text" name="time"/>
<input type="button" name="ssbutton" value="Start/Stop" onclick="javascript:startstop()"/>
<input type="button" name="reset" value="Reset" onclick="javascript:swreset()"/>
</div>
</form>
</body>
</html>
I am new with javascript ,Where I am doing mistake?? Thank You…
Udate:Finally I get the errors and solved it. Here is updates solution for someone who want to implement JavaScript Stopwatch in C# Application.
<html>
<head>
<script type="text/javascript">
var ms = 0;
var state = 0;
var then;
var now;
ms = new Date();
function startstop()
{
if (state == 0)
{
state = 1;
then = new Date();
then.setTime(then.getTime() - ms);
}
else
{
state = 0;
now = new Date();
ms = now.getTime() - then.getTime();
document.getElementById('timeInput').value = ms;
}
}
function swreset() {
state = 0;
ms = 0;
document.getElementById('timeInput').value = ms;
}
function display() {
ms = new Date();
setTimeout("display();", 50);
if (state == 1) {
now = new Date();
ms = now.getTime()-then.getTime();
document.getElementById('timeInput').value = ms;
}
}
</script>
</head>
<body onload="display()">
<form>
Time:
<input type="text" name="time" id="timeInput"/>
<input type="button" name="ssbutton" value="Start/Stop" onclick="javascript:startstop()"/>
<input type="button" name="reset" value="Reset" onclick="javascript:swreset()"/>
</form>
</body>
</html>
Thanks For All your help.
Here are some first steps: (This doesn’t mean that everything will work, but it will sure help.)
<input type="text" name="time" id="timeInput"/>document.getElementById("timeInput").value = ...alert(...)statements in the javascript to see where its going and what values it’s coming up with.