Skip to content

Instantly share code, notes, and snippets.

@fsboehme
Forked from Joel-James/countdown-redirect.html
Last active December 11, 2020 15:44
Show Gist options
  • Save fsboehme/9a368994aee1cc4dbff7dbf0a14a0f79 to your computer and use it in GitHub Desktop.
Save fsboehme/9a368994aee1cc4dbff7dbf0a14a0f79 to your computer and use it in GitHub Desktop.
Countdown Redirect Using JavaScript - with Cancel
<!-- Modify this according to your requirement -->
<p id="countdown-wrapper">
Redirecting in <span id="countdown">10</span> seconds.
<a href="#" onclick="stopCountdown();return false;">Cancel</a>
</p>
<!-- JavaScript part -->
<script type="text/javascript">
// Total seconds to wait
var seconds = 10;
var countdownTimer;
function countdown() {
seconds = seconds - 1;
if (seconds < 0) {
// Chnage your redirection link here
window.location = "https://example.com";
} else {
// Update remaining seconds
document.getElementById("countdown").innerHTML = seconds;
// Count down using javascript
countdownTimer = window.setTimeout("countdown()", 1000);
}
}
function stopCountdown() {
window.clearTimeout(countdownTimer)
document.getElementById('countdown-wrapper').style.display = 'none'; // or .fadeout() with jquery
}
// Run countdown function
countdown();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment