A Pen by Joel Alejandro Villarreal Bertoldi on CodePen.
Created
September 5, 2020 00:06
-
-
Save joelalejandro/1bc61844baf00094a6d7edd5beb6ed7b to your computer and use it in GitHub Desktop.
Ejecución diferida vs. cíclica vs. "falsa cíclica" vs. cíclica condicionda
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
<div id="contador">1</div> |
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
let contador = 1; | |
const elementoContador = document.querySelector("#contador"); | |
function subirContador() { | |
contador += 1; | |
elementoContador.innerHTML = contador; | |
} | |
// Ejecución diferida | |
setTimeout(subirContador, 1000); | |
// Ejecución cíclica | |
setInterval(function() { | |
contador += 1; | |
elementoContador.innerHTML = contador; | |
}, 1000); | |
// Ejecución "cíclica" (con setTimeout) | |
setTimeout(subirContador, 1000); | |
setTimeout(subirContador, 2000); | |
setTimeout(subirContador, 3000); | |
setTimeout(subirContador, 4000); | |
setTimeout(subirContador, 5000); | |
// Ejecución cíclica condicionada | |
let ciclo = setInterval(function() { | |
if (contador === 10) { | |
clearInterval(ciclo); | |
return; | |
} | |
contador += 1; | |
elementoContador.innerHTML = contador; | |
}, 1000); |
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 { | |
text-align: center; | |
font-size: 260px; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment