Last active
November 16, 2017 20:14
-
-
Save joalbertg/a9a9899b27859471939381c4b1cf3bcb to your computer and use it in GitHub Desktop.
Escribiendo con efecto máquina
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* contador regresivo | |
** parametro ms númerico, representa milisegundos | |
*/ | |
var countDown = function (ms) { | |
// referencia al método setInterval(callback, time) | |
var myCountDown = setInterval(function () { | |
// variables para minutos y para segundos | |
var minutes = Math.floor(ms / (1000 * 60)), | |
seconds = Math.floor((ms % (1000 * 60)) / 1000); | |
// usando ES6 | |
/* document.body.innerHTML = `<h1>Quedan ${minutes} minutos y ${seconds} segundos</h1>`; */ | |
// usando ES5 | |
document.body.innerHTML = '<h1>Quedan ' + minutes + ' minutos y ' + seconds + ' segundos</h1>'; | |
// si ms llega a 0, se elimina el método setInterval(callback, time) | |
// y se muestra el string: Tiempo cumplido!!! | |
if(ms === 0) { | |
clearInterval(myCountDown); | |
document.body.innerHTML = '<h1>Tiempo cumplido!!!</h1>'; | |
} | |
// se le resta 1 segungo al total de milisegundos | |
ms -= 1000; | |
// el método setInterval se ejecuta cada segundo | |
}, 1000); | |
} | |
// asignamos 1h -> 60 min | |
countDown(3600000); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment