Javascript relojes y repeticiones

Usar setInterval(función, milisegundos) para ejecutar una función con un retraso de milisegundos.
No lo hace repetidamente, pero una estrategia común es ejecutar el código y llamar a setTimeout al final del script.

<!DOCTYPE html>
<html>
<head>
<script>
function startTime() {
var today = new Date();
var h = today.getHours();
var m = today.getMinutes();
var s = today.getSeconds();
m = checkTime(m);
s = checkTime(s);
document.getElementById(‘txt’).innerHTML =
h + «:» + m + «:» + s;
var t = setTimeout(startTime, 500);
}
function checkTime(i) {
if (i < 10) {i = «0» + i};  // add zero in front of numbers < 10
return i;
}
</script>
</head>

<body onload=»startTime()»>

<div id=»txt»></div>

</body>
</html>

Cuenta Atrás

var count=30;

var counter=setInterval(timer, 1000);

function timer()
{
count=count-1;
if (count &lt;= 0)
{
clearInterval(counter);
//termina el contador
return;
}

//Aquí el código para mostrar los segundos
document.getElementById(‘contador’).innerHTML = count;
}

<div id=»contador»></div>

Write a Reply or Comment

Your email address will not be published.