Last active
August 14, 2019 16:27
-
-
Save hegemanjr/167fb31426a0786852936829155af8d9 to your computer and use it in GitHub Desktop.
Count Down Redirect Using JavaScript and PHP
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
<?php | |
/** | |
* These constants can be set elsewhere in your project, ideally in a configuration file. | |
*/ | |
if ( ! defined( 'COUNTDOWN_REDIRECT_LOCATION' ) ) { | |
define('COUNTDOWN_REDIRECT_LOCATION', 'https://google.com'); | |
} | |
if ( ! defined( 'COUNTDOWN_REDIRECT_SECONDS' ) ) { | |
define('COUNTDOWN_REDIRECT_SECONDS', 15); | |
} | |
?> | |
<!-- Modify this according to your requirement --> | |
<h3> | |
Redirecting to <a href="<?=COUNTDOWN_REDIRECT_DESTINATION;?>"><?=COUNTDOWN_REDIRECT_DESTINATION;?></a> after <span id="countdown"><?=COUNTDOWN_REDIRECT_SECONDS;?></span> seconds. | |
</h3> | |
<!-- JavaScript part --> | |
<script type="text/javascript"> | |
// Total seconds to wait | |
var seconds = <?=COUNTDOWN_REDIRECT_SECONDS;?>; | |
var destination = "<?=COUNTDOWN_REDIRECT_DESTINATION;?>"; | |
function countdown() { | |
seconds = seconds - 1; | |
if (seconds < 0) { | |
// Chnage your redirection link here | |
window.location = destination; | |
} else { | |
// Update remaining seconds | |
document.getElementById("countdown").innerHTML = seconds; | |
// Count down using javascript | |
window.setTimeout("countdown()", 1000); | |
} | |
} | |
// Run countdown function | |
countdown(); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment