Page 1 of 2 12 LastLast
Results 1 to 10 of 14

Thread: counting seconds

  1. #1
    Join Date
    Sep 2007
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default counting seconds

    i am searching some script that counting seconds...
    for example
    1..2..3..4..5..6..7..8..9..10!
    stopes after 10 and do nothing....

    has someone cross over script like this?

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

    Default

    Erm...
    Code:
      <script type="text/javascript">
        function count(el, start, stop) {
          el = el.firstChild || el.appendChild(document.createTextNode(""));
          start = start || 0;
          stop = stop || 10;
    
          var c = start,
            inc = function() {
              if (c > stop) {
                clearInterval(t);
                el = null;
              } else
                el.nodeValue = c++;
            },
            t = setInterval(inc, 1000);
        }
      </script>
    </head>
    <body>
      <p onclick="count(this);">Click here to begin counting.</p>
    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!

  3. #3
    Join Date
    Sep 2007
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    i already have a script:

    Code:
    <body onLoad="setTimeout('check_Redirect()', 30000);">
                     
    <script type="text/javascript">
    function check_Redirect(){
                   document.getElementById("alertmsg").innerHTML = "<p style='background:#000000;color:#ffffff;'>You have 10 seconds left!"</p>;
                           }
    
    <div id="alertmsg" name="alertmsg"></div>
         </script>
    i need to add this counting function inside of this script in order that when the timeout will shoot, that the seconds will strat running.

    i tried to fit this script to my script but it not working...just doing nothing...can you show me how to combine them ?

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

    Default

    Code:
      <style type="text/css">
        .timer {
          color: white;
          background-color: black;
        }
      </style>
      <script type="text/javascript">
        function count(el, start, stop) {
          el = el.firstChild || el.appendChild(document.createTextNode(""));
          start = start || 0;
          stop = stop || 10;
    
          var c = start,
            inc = function() {
              if (c > stop) {
                clearInterval(t);
                el = null;
              } else
                el.nodeValue = c++;
            },
            t = setInterval(inc, 1000);
        }
    
        onload = function() {
          setTimeout(function() { count(document.getElementById("timer")); }, 30000);
        };
      </script>
    </head>
    <body>
      <p class="timer">You have <span id="timer">plenty of time</span> left.</p>
    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!

  5. #5
    Join Date
    Sep 2007
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    twey, i need to use
    Code:
    <body onLoad="setTimeout('check_Redirect()', 30000);">
    and the counting function got to be part of this script

    how can i do this?

  6. #6
    Join Date
    Sep 2007
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    please look at my script and tell me where i got wrong...

    Code:
    <body onLoad="setTimeout('if(c==null) c=setInterval('startClock()',1000);', 50000);">
                      <script type="text/javascript">
                            var c=null;
                            var ctr=10;
                            function startClock(theLink){
                                  var o=document.getElementById('sec');
                                  var s=ctr<10?" second":" seconds";
                                  o.innerHTML="<p style='background:#000000;color:#ffffff;'>You have "+(ctr) +s +" left</p>";
                                  ctr--;
                                  if(ctr<0){clearInterval(c); return false;}
    
                            }
    
    
                      </script>
                </head>
    <body>
          <div id="sec"></div>
    if i call the script like this;
    <input type="button" value="Start" onClick="if(c==null) c=setInterval('startClock()',1000);">
    it is working
    but i need it to be called from the timeout as i tried above...

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

    Default

    The snippet of code you provided uses a couple of bad practices, namely inline event handlers and passing strings to setTimeout(). Additionally, camel-casing "onload" breaks compatibility with XHTML for no reason. I suggest dropping it in favour of my script-assigned event handler. Your attempt also uses inline style and innerHTML, again signs of bad coding: inline style causes unnecessary duplication and mitigates a lot of the benefits of using CSS in the first place, and innerHTML is non-standard and will not work in XHTML pages, unnecessarily reducing reliability and portability.
    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!

  8. #8
    Join Date
    Sep 2007
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default twey...i agree with u...

    then how we use your code inside the check_Redirect()
    that will be called with
    Code:
    <body onLoad="setTimeout('check_Redirect()', 30000);">
    ?

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

    Default

    Don't call it like that. It's bad. It does the equivalent already here:
    Code:
        onload = function() {
          setTimeout(function() { count(document.getElementById("timer")); }, 30000);
        };
    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!

  10. #10
    Join Date
    Sep 2007
    Posts
    58
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Code:
      <script type="text/javascript">
        function count(el, start, stop) {
          el = el.firstChild || el.appendChild(document.createTextNode(""));
          start = start || 10;
          stop = stop || 0;
    
          var c = start,
            inc = function() {
              if (c < stop) {
                clearInterval(t);
                el = null;
              } else
                el.nodeValue = c--;
            },
            t = setInterval(inc, 1000);
        }
    
        onload = function() {
          setTimeout(function() { count(document.getElementById("timer")); }, 10000);
        };
      </script>
    </head>
    <body>
      <div id="timer"></div>
    i am using "div"
    i want that it will apear on screen only when the timeout over...
    +i want that there will be a black background=#000000 and white font #ffffff
    how i can add those things?

    if this script can be run from <body onLoad="setTimeout('????()', 30000);">
    i will be happy to know how
    Last edited by hantz; 11-12-2007 at 10:04 AM.

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
  •