Log in

View Full Version : Counter



tobolate
11-03-2007, 06:01 PM
hey im working on my websites 404 page, and it re directs you to the home page, id like to have a little counter at the bottom of the page that counts down in seconds from 10. so that they can see when they will be redirected. does anyone have something like this?

Trinithis
11-03-2007, 06:15 PM
<div id="countDiv">10</div>

<script type="text/javascript">
(function() {
var cdiv = document.getElementById("countDiv");
var n = 10;
var tim = window.setInterval(function() {
cdiv.firstChild.nodeValue = --n;
if(n <= 0) {
window.clearInterval(tim);
//do other stuff as well
}
}, 1000);
})();
</script>

Twey
11-03-2007, 07:08 PM
Careful not to trap DOM references in closures. This will lead to memory leaks in IE. You should probably look it up each time:
(function() {
var cdiv = function() {
return document.getElementById("countDiv");
},
n = 10,
tim = setInterval(function() {
cdiv().firstChild.nodeValue = --n;
if(n <= 0) {
clearInterval(tim);
// &c.
}
}, 1000);
})();Of course, it doesn't make much difference here anyway, since the page is redirected pretty quickly. Always good practice though.

Trinithis
11-03-2007, 08:03 PM
Well thanks anyway. I didn't know that. As a matter of fact, I don't really know what patterns cause DOM memory leaks . . . just that DOM interaction with Javascript can cause leaks.