+ Reply to Thread
Results 1 to 5 of 5

Thread: Count Down

  1. #1
    WarriorH is offline x10Hosting Member WarriorH is an unknown quantity at this point
    Join Date
    Feb 2008
    Posts
    55

    Count Down

    Hi guys, I need a VERY simple count down. I need something to count down 20 minutes, then loop. I need this as a way to show users how much longer they have until database clears.

  2. #2
    marshian's Avatar
    marshian is offline x10 Elder marshian is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    Belgium
    Posts
    526

    Re: Count Down

    Add this to your <head>:
    HTML Code:
    <script type="text/javascript">
    	//How much time there is on the timer when the page has loaded? (in seconds)
    	var timeleft = 20*60
    	//How much time does the timer have to reset to? (in seconds)
    	var resettime = 20*60;
    	function countdown() {
    		setTimeout(countdown, 1000);
    		if(timeleft > 0) {
    			timeleft--;
    		} else {
    			timeleft = resettime;
    		}
    		document.getElementById("time").innerText = ((timeleft-timeleft%60)/60)+":";
    		if(timeleft%60 < 10) document.getElementById("time").innerText += "0";
    		document.getElementById("time").innerText += ""+timeleft%60;
    		document.getElementById("time").textContent = document.getElementById("time").innerText;
    	}
    	function startTimer() {
    		timeleft++;
    		countdown();
    	}
    </script>
    add onload="startTimer()" to your <body> tag
    eg
    HTML Code:
    <body onload="startTimer()">
    and then use a <span> with id "time" where you want to put your timer.
    eg
    HTML Code:
    <span id="time"></span>
    Tested in IE and FF (omfg, WILL YOU FINALLY WORK?!)

    Marshian
    Last edited by marshian; 04-28-2008 at 11:51 AM.
    Real programmers don't document their code - if it was hard to write, it should be hard to understand.

  3. #3
    woiwky is offline x10 Lieutenant woiwky is an unknown quantity at this point
    Join Date
    Mar 2008
    Posts
    390

    Re: Count Down

    That code looks alright for the most part. The only thing is that the OP said that after 20 minutes, the db is cleared. This sounds to me like a cron job rather than something that would rely on the clients. In this case it would be a good idea to have php store the timestamp of the last clearing in the db(in a table that isn't cleared, of course), then have that timestamp loaded into the JS so it can count down from the approximate time remaining. That's basically just changing

    var timeleft = 20*60;

    to

    var timeleft = <?php print (($t = $lastclear + 1200 - time()) > 1200 ? 1200 : $t);?>;

    where $lastclear is the time of the last clearing retrieved from the db.
    "But you have access to the greatest source of knowledge in the universe."
    "Well I do talk to myself sometimes, yes."

    "I'm back, and I'm bad! Obviously within certain, sensible, preset parameters"

  4. #4
    marshian's Avatar
    marshian is offline x10 Elder marshian is an unknown quantity at this point
    Join Date
    Jan 2008
    Location
    Belgium
    Posts
    526

    Re: Count Down

    Yes, I realised this, but he said very simple, so I supposed he just wanted an estimation.
    Real programmers don't document their code - if it was hard to write, it should be hard to understand.

  5. #5
    WarriorH is offline x10Hosting Member WarriorH is an unknown quantity at this point
    Join Date
    Feb 2008
    Posts
    55

    Re: Count Down

    thanks alot guys, this is exactly what i needed.

+ Reply to Thread

Similar Threads

  1. Noah's Classifieds ad count issue
    By HeavyMetalMan in forum Scripts & 3rd Party Apps
    Replies: 0
    Last Post: 12-13-2007, 12:43 PM
  2. Minimum post count to apply for hosting
    By -Daniel in forum Feedback and Suggestions
    Replies: 17
    Last Post: 07-15-2005, 07:00 PM
  3. [PHP] Member Count Script
    By Deshi in forum Tutorials
    Replies: 10
    Last Post: 06-22-2005, 06:57 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers