Skip to content

Instantly share code, notes, and snippets.

@dlucero23
Created April 11, 2016 14:26
Show Gist options
  • Save dlucero23/2b076bd99d7d14dcb2fc6cc595c38d83 to your computer and use it in GitHub Desktop.
Save dlucero23/2b076bd99d7d14dcb2fc6cc595c38d83 to your computer and use it in GitHub Desktop.
JS 5 minute countdown script. Great for adding to a landing pages where you would like the contact to take a specific action within a short amount of time.
<body>
<div>Registration closes in <span id="time">05:00</span> minutes!</div>
</body>
function startTimer(duration, display) {
var timer = duration, minutes, seconds;
setInterval(function () {
minutes = parseInt(timer / 60, 10)
seconds = parseInt(timer % 60, 10);
minutes = minutes < 10 ? "0" + minutes : minutes;
seconds = seconds < 10 ? "0" + seconds : seconds;
display.text(minutes + ":" + seconds);
if (--timer < 0) {
timer = duration;
}
}, 1000);
}
jQuery(function ($) {
var fiveMinutes = 60 * 5,
display = $('#time');
startTimer(fiveMinutes, display);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment