Skip to content

Instantly share code, notes, and snippets.

@carlsednaoui
Last active December 17, 2015 03:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save carlsednaoui/5547573 to your computer and use it in GitHub Desktop.
Save carlsednaoui/5547573 to your computer and use it in GitHub Desktop.
Simple JS timer created for GA class.
// $(document).ready(function() {
// var counter = timer(90); // time in seconds
// var status = "active";
// function timer(count) {
// var intervalID = setInterval(function() {
// $('.timer').text(status + ": " + count);
// count -= 1;
// if (count <= 0 && status == "active") {
// clearInterval(intervalID);
// status = "rest";
// timer(30); // time in seconds
// } else if (count <= 0 && status == "rest") {
// clearInterval(intervalID);
// status = "active";
// timer(90); // time in seconds
// }
// }, 1000);
// }
// });
$(document).ready(function() {
(function timer(count, status) {
var intervalID = setInterval(function() {
document.querySelectorAll('.timer')[0].innerHTML = (status + ": " + Math.floor(count/ 60) + ":" + (count % 60));
count -= 1;
if (count <= 0 && status == "active") {
clearInterval(intervalID);
timer(30, "rest");
} else if (count <= 0 && status == "rest") {
clearInterval(intervalID);
timer(150, "active");
}
}, 1000);
})(10, "active");
});
// Requires the page to have something like this: <p class="timer">Ready?</p>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment