Jarnail Singh
Jarnail Singh

Reputation: 85

set the value of time to zero in stop watch using javascript

<script>
function stop()
{
d=new Date();
d.setHours(0,0,0,0);
document.getElementById("time").value = d;
}
</script>
<form>
<input type=button value="stop" onclick="stop()">
</form>

I want to reset the time of watch using stop button but the above code is not working.I have also tried directly putting string("00:00:00") instead of using setHours function.

Upvotes: 1

Views: 168

Answers (4)

Vlad Ankudinov
Vlad Ankudinov

Reputation: 2046

You might do something like

document.getElementById("time").value = d.getHours() + ":" + d.getMinutes() + ":" + d.getSeconds();

check out this doc for details https://msdn.microsoft.com/en-us/library/ff743760(v=vs.94).aspx

Upvotes: 1

Mohan Goud
Mohan Goud

Reputation: 27

You can use the following function in java script for stoping the timer.

/* Stop button */
stop.onclick = function() {
    clearTimeout(t);
}

Upvotes: 1

GMchris
GMchris

Reputation: 5648

That method should work for setting a date object to 00:00:00, however if you're trying to implement a stopwatch, Date might not be the best choice as it has a lot more information than just an hour, minutes and seconds. Try incrementing a single variable every second with setInterval, and formatting the time using a custom timer function.

Upvotes: 1

dieuhd
dieuhd

Reputation: 1326

You cant set a object Date for Element #time, You can try: document.getElementById("time").value = d.getHours() + ":" + d.getMinutes() + ":" + getSeconds();

Upvotes: 1

Related Questions