Some time we want to
perform some action after particular interval of time. Ex. redirect to other
page after 5 sec . This can be done by using javaScript.
Javascript Code to redirect
after 5 sec :
<script type="text/javascript">
function Redirect()
{
var sec
= 5;
var dvCount
= document.getElementById("dvCount");
var lblCount
= document.getElementById("lblCount");
dvCount.style.display = "block";
lblCount.innerHTML = sec ;
setInterval(function () {
sec --;
lblCount.innerHTML = sec ;
if (sec
== 0) {
dvCount.style.display = "none";
window.location = "http://www.google.com/";
}
}, 1000);
}
</script>
Sample Code to Redirect
after 5 sec :
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
</head>
<body>
<fieldset style="width:400px;"><legend><strong>Redirect to other Page After 5 Second Using JavaScript</strong></legend>
<br /> Click here to redirect Page
<input type="button" value="Redirect" onclick="Redirect()" />
<br />
<br />
<div id="dvCount" style = "display:none">
You will
be redirected after <span id = "lblCount"></span> sec .
</div></fieldset>
<script type="text/javascript">
function Redirect()
{
var sec
= 5;
var dvCount
= document.getElementById("dvCount");
var lblCount
= document.getElementById("lblCount");
dvCount.style.display = "block";
lblCount.innerHTML = sec ;
setInterval(function () {
sec --;
lblCount.innerHTML = sec ;
if (sec
== 0) {
dvCount.style.display = "none";
window.location = "http://www.google.com/";
}
}, 1000);
}
</script>
</body>
</html>