Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    countdown timer! help please!!!

    hi,

    i am currently trying to write a javascript program;

    it would be for a library reservation system. this would be put on the computers with sessions from 15 minutes to 2 hours (in 15 minute chunks i.e, 15, 30, 45, 1 hour).

    this program would countdown the remaining time on their session, with pop-up messages appearing every so often.

    at the end of the session, the program will have to automatically log the user off.

    i am a student studying javascript, and haven't got any idea how to even start

    can anyone give me a helping hand to get me started.

    thanks

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,898
    Thanks
    203
    Thanked 2,530 Times in 2,508 Posts
    Try this:-

    Code:
    <html>
    <head>
    
    <script type = "text/javascript">
    
    var timeInSecs;
    var ticker;
    
    function start(){
    var s = document.getElementById("period").value;
    startTimer(s);
    }
    
    function startTimer(secs){
    timeInSecs = parseInt(secs);
    ticker = setInterval("tick()",1000); 
    tick(); // to start counter display right away
    }
    
    function tick() {
    var secs = timeInSecs;
    if (secs == 299) {
    alert ("You have only 5 minutes left");
    }
    if (secs>0) {
    timeInSecs--;
    showTime(secs);
    }
    
    else {
    clearInterval(ticker); // stop counting at zero
    showTime(0);
    alert ("Your time is up!");
    window.location = "http://www.google.com";  
    }
    }
    
    function showTime(secs) {
    var hours= Math.floor(secs/3600);
    secs %= 3600;
    var mins = Math.floor(secs/60);
    secs %= 60;
    var result = ((hours < 10 ) ? "0" : "" ) + hours + ":" + ( (mins < 10) ? "0" : "" ) + mins
                      + ":" + ( (secs < 10) ? "0" : "" ) + secs;
    document.getElementById("countdown").innerHTML = result;
    }
    
    </script>
    
    </head>
    
    <select id = "period" onchange = "start()">
    <option value = "0">Select time required</option>
    <option value = "900">15 minutes</option>
    <option value = "1800">30 minutes</option>
    <option value = "2700">45 minutes</option>
    <option value = "3600">60 minutes</option>
    <option value = "4500">75 minutes</option>
    <option value = "5400">90 minutes</option>
    <option value = "6300">105 minutes</option>
    <option value = "7200">120 minutes</option>
    </select>
    
    <span id="countdown" style="font-weight: bold;"></span>
    
    </body>
    </html>
    Note that the user can simply restart the counter again! You might perhaps disable the select list once a selection has been made, and enable it again once the selected time has expired.

    i raed a wlihe ago taht as lnog as the frsit and lsat lttesrs in a wrod are in the cerroct pcale msot of us wulod siltl be albe to raed bceause our barnis jsut looks at the frsit and lsat lttesrs.
    Last edited by Philip M; 09-07-2010 at 04:27 PM.

  • Users who have thanked Philip M for this post:

    decade10 (09-08-2010)


  •  

    Posting Permissions

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