Many web pages have a similar effect, that is, after a specified time to jump to other pages, if the humanization of a little words, will have a countdown effect, this chapter describes how to achieve this effect, code examples are as follows:
<script type= "Text/javascript" >
var t=10;
SetInterval ("refer ()", 1000);
function refer () {
if (t==0) {
location.href= "http://www.jb51.net";
}
document.getElementById (' show '). Innerhtml= "+t+" seconds later jump to cloud-dwelling community ";
t--;
}
</script>
<span id= "Show" ></span>
The above code to achieve the desired effect, can be in 10 seconds to jump to the specified page, the following introduction to the implementation process.
I. Principle of realization:
The principle is very simple, is to use the setinterval () Timer function, every second to perform a refer () function, this function can not perform a T minus 1, while the current T value to write Div, if T to 0, that is, the countdown is complete, jump to the specified page. The principle is roughly the same.
The 1.setInterval () function can refer to the SetInterval () section of the functional usage.
2.location.href can refer to the HREF attribute section of the Location object.
The 3.innerHTML attribute can refer to the usage section of the innerHTML property of JS.
below to share 2 simple jump code, do a summary, a variety of timing jump code records are as follows:
(1) using the SetTimeout function to achieve a timed jump (the following code to write in the body area)
<script type= "Text/javascript" >
//3 seconds later jumps to the specified page
settimeout (window.location.href= ' http:// Www.jb51.net ', 3);
</script>
(2) HTML code implementation, the page in the head area of the block with the following code
<!--5 Seconds to jump to the specified page-->