Created
October 5, 2015 16:32
-
-
Save geekforbrains/a1a7d6d2e8dc095891e3 to your computer and use it in GitHub Desktop.
Javascript countdown
This file contains hidden or 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
#countdown { | |
position: absolute; | |
left: 50%; | |
top: 50%; | |
width: 100px; | |
height: 100px; | |
display: none; | |
margin: -50px 0 0 -50px; | |
border: 2px solid #cecece; | |
background-color: #fefefe; | |
text-align: center; | |
line-height: 100px; | |
font-size: 60px; | |
font-family: sans-serif; | |
} |
This file contains hidden or 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
<a id="startCountdown" href="#">Start</a> | |
<div id="countdown"></div> |
This file contains hidden or 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
function doCountdown() { | |
var start = 1; | |
var finish = 3; | |
var message = "Go"; | |
var url = "http://google.com"; | |
var colors = [ | |
"green", // 1 | |
"blue", // 2 | |
"orange", // 3 | |
"red" // Go | |
]; | |
var element = document.getElementById("countdown"); | |
element.style.display = "block"; | |
var loopCountdown = function(count) { | |
element.innerHTML = ''+count; | |
element.style.color = colors[count-1]; | |
setTimeout(function() { | |
console.log(count); | |
if(count < finish) { | |
loopCountdown(++count); | |
} else { | |
element.innerHTML = message; | |
element.style.color = colors[colors.length - 1]; | |
window.location.replace(url); | |
} | |
}, 1000); | |
} | |
loopCountdown(start); | |
} | |
document.getElementById("startCountdown").onclick = doCountdown; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment