Hola a todos.
Necesito hacer un contador de tiempo en mi aplicación con javascript.
Actualmente lo tengo casi terminado con el siguiente código:
Código:
//CODIGO JAVASCRIPT
var timerID = 0;
var tStart = null;
function UpdateTimer() {
if(timerID) {
clearTimeout(timerID);
clockID = 0;
}
if(!tStart)
tStart = new Date();
var tDate = new Date();
var tDiff = tDate.getTime() - tStart.getTime();
tDate.setTime(tDiff);
document.act.theTime.value = ""
+ tDate.getMinutes() + ":"
+ tDate.getSeconds();
timerID = setTimeout("UpdateTimer()", 1000);
}
function Start() {
tStart = new Date();
document.act.theTime.value = "00:00";
timerID = setTimeout("UpdateTimer()", 1000);
}
function Stop() {
if(timerID) {
clearTimeout(timerID);
timerID = 0;
}
tPause = tStart;
tStart = null;
}
function Reset() {
tStart = null;
document.act.theTime.value = "00:00";
}
Código HTML:
//CODIGO HTML
<form name="act" method="post">
<table width="200" align="center" border="0" cellspacing="2" cellpadding="2">
<tr>
<td class="label">Tiempo</td>
<td><input type=text name="theTime" size=5></td>
</tr>
<tr>
<td colspan="4"><center><input type=button name="start" value="Inicio" onclick="Start()">
<input type=button name="stop" value="Parar" onclick="Stop()">
<input type=button name="reset" value="Reset" onclick="Reset()"></center></td>
</tr>
</table></form>
<script>
Reset();
Start();
</script>
La cuestión es que no soy capaz de crear el boton PAUSE, es decir, que pare el contador de tiempo cuando lo pulso, y continue contando cuando lo vuelvo a pulsar (me da igual hacerlo con un mismo botón que con dos).
Muchas gracias por adelantado, la verdad es que me trae de cabeza, saludos.