Results 1 to 4 of 4

Thread: Counter

  1. #1
    Join Date
    Apr 2007
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Counter

    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?

  2. #2
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    Code:
    <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>
    Trinithis

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    Careful not to trap DOM references in closures. This will lead to memory leaks in IE. You should probably look it up each time:
    Code:
    (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.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default

    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.
    Trinithis

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •