Skip to content

Instantly share code, notes, and snippets.

@pama
Last active August 29, 2015 14:11
Show Gist options
  • Save pama/b99e1ceaa849b6a67c05 to your computer and use it in GitHub Desktop.
Save pama/b99e1ceaa849b6a67c05 to your computer and use it in GitHub Desktop.
Countdown timer example (not accurate, though)
<html>
<head>
<script src="http://code.jquery.com/jquery-2.1.1.js"></script>
</head>
<body>
<div id="timer"></div>
<script>
var time = 3; // in seconds
setInterval(function () {
if(time == 0) {
window.location = "http://www.google.com";
// if you don't want to record page change in browser's history, use replace
// window.location.replace("http://www.google.com");
}
// show the times in the browser
set_counter(time)
// update time
time = time - 1;
// if time is zero, the party is over and we need to redirect
}, 1000); // called every second
/** time in seconds **/
function set_counter(time) {
var minutes = Math.floor(time / 60);
var seconds = time - minutes * 60;
$('#timer').text(format_number(minutes) + ":" + format_number(seconds));
}
/**
returns a sero filled string representation of a 2 algarism number with a left zero
eg: number 4 will be returned as "04"
*/
function format_number(number) {
return (number < 10) ? ("0" + number) : number;
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment