
Javascript countdown and redirect after a few seconds
Javascript implements a countdown to redirect to the specified page. Since variables(such as "jump after a few seconds") that should be defined as arguments of method must be defined as global variables, they cannot be written into a method, which makes the call very inconvenient and the values that are frequently changed cannot be passed with arguments, and the initial value in the code must be changed. For those who do not understand the code, the modification may be wrong, which is very inconvenient to use. Therefore, it is necessary to encapsulate it into an object, define all variables and methods in this object, the values that need to be changed can be defined as the arguments of the method in the object, so that you can avoid modifying the code, just pass the value to be changed to the method of the object.
I. Implementation process:
1. First define an object jump, and then define 4 variables, the meaning of the representative, there are comments in the code; Secondly, define an initialization method (init) and a method(show) to display jump prompt information.
2. The init method is responsible for assigning the passed value to the variable of the object and creating a timer(setInterval), which specifies that the "show" method is executed once per second.
3. The "show" method first gets the object(i.e. div tag id=showId) that displays the jump message, and then assigns the remaining jump time, prompt text, and jump URL to the "innerHTML" attribute of the object; then determines whether the remaining jump time is equal to 0, if it is equal to 0, then clear the timer and jump to the specified page; the last is the jump time minus 1(jump.s--).
II. The implementation code is as follows:
<html>
<head>
<title>Javascript countdown and redirect after a few seconds</title>
<script language="javascript" type="text/javascript">
var jump = {
o: null,//Display the object that prompts the jump information(such as "show" in the example)
s: 0,//Jump time(unit: seconds)
url: "",//Jump URL
intervalId: null,//Timing object
init: function (obj, time, url) {
jump.o = obj;
jump.s = time;
jump.url = url;
jump.intervalId = setInterval("jump.show()", 1000);
},
show: function () {
document.getElementById(jump.o).innerHTML = "Automatically jump to after " + jump.s + " seconds " + jump.url;
if (jump.s == 0) {
clearInterval(jump.intervalId);
window.location = jump.url;
}
jump.s--;
}
};
jump.init("showId", 5, "http://www.liangshunet.com/");
</script>
</head>
<body>
<div id="showId"></div>
</body>
</html>
Call(Redirect after 5 seconds): jump.init("showId", 5, "http://www.xxx.com/");
Arguments description of jump.init(obj, time, url):
obj: Object to display jump prompt information(<div id="showId"></div> in the example);
time: Jump time (unit: second);
url: The URL to be redirected to.
III. The implementation code of a little higher efficiency:
It is basically the same as the above method, the only difference is: only get the object that displays the prompt jump information(initialized in the init method), and the above method is to get it once every second, the efficiency is basically negligible if the number of seconds is small. The code show as below:
<html>
<head>
<title>Javascript redirect after 5 seconds with countdown</title>
<script language="javascript" type="text/javascript">
var jump = {
o: null,//Display the object that prompts the jump information(such as "show" in the example)
s: 0,//Jump time(unit: seconds)
url: "",//Jump URL
intervalId: null,//Timing object
init: function (obj, time, url) {
jump.o = document.getElementById(obj);//Differences from method one (1)
jump.s = time;
jump.url = url;
jump.intervalId = setInterval("jump.show()", 1000);
},
show: function () {
jump.o.innerHTML = "Automatically jump to after " + jump.s + " seconds " + jump.url;//Differences from method one (2)
if (jump.s == 0) {
clearInterval(jump.intervalId);
window.location = jump.url;
}
jump.s--;
}
};
jump.init("showId", 5, "http://www.xxx.com/");
</script>
</head>
<body>
<div id="showId"></div>
</body>
</html>
Pay attention to the problem: the calling code must be placed behind the object that displays the prompt jump information(such as <div id="showId"></div> in the example), otherwise the object cannot be found during initialization.
-
Related Reading
- Javascript multidimensional array (create, add and r
- Javascript trim string whitespace, with removing lef
- Remove text after clicking in the textbox and then d
- Javascript get referrer url(previous page url), curr
- Javascript get current url, domain from url, relativ
- Javascript convert hex to decimal, binary to hex, oc
- Javascript delay loading images to improve web page
- Remove html element javascript by their parent or cl
- Difference between substr and substring in javascrip
- Javascript split usage and examples, with splitting
- Javascript get all input elements in form and all im
- Javascript sets the element height that cannot be au