This article is about Javascript/Jquery redirection after countdown, which is 10 seconds in this example.
Our output will be like:
Javascript Redirection
Firstly, we need to Javascript redirection. When the following Javascript code block runs, we will be redirected to reitix.com (surely you can change the url).
window.location = "http://reitix.com";
Jquery Countdown
Secondly, wee need a javascript countdown algorithm. To solve this little problem, we will user setInterval method and trigger our update method at each interval (1 seconds) completes. Here we go:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function () {
var doUpdate = function () {
var i = parseInt($("#counter").html());
if (i !== 0) {
$("#counter").html(i - 1);
}
else
{
window.location = "http://reitix.com";
}
};
setInterval(doUpdate, 1000);
});
</script>
Further, if we want to update our html with each countdown:
<div>
You will be redirected after:
<div id="counter">10</div> seconds
</div>
Done, with these codes, after 10 seconds, we will be redirected to reitix.com.