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 7 of 7
  1. #1
    New Coder
    Join Date
    May 2009
    Posts
    58
    Thanks
    8
    Thanked 4 Times in 4 Posts

    Counting up timer(either need to code, or idea of what to do.)

    Ok, so here is my problem. I want to a user to be timed on how long the view a page. I currently am thinking that once they view the page a function will start the timer, and put the start time in a hidden input box and then when the user transfers to the next page using a submit button the end time will be put into a hidden input box. I would then just subtract the two. But I am not seeming to be able to get the end time. Any ideas?
    Last edited by Rihoj; 05-29-2009 at 07:00 AM.

  • #2
    New Coder
    Join Date
    Nov 2008
    Location
    New Delhi,India
    Posts
    26
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Smile Countdown timer

    Probably this can help http://www.vijayjoshi.org/2009/03/23...in-javascript/

    Please let us know.

  • Users who have thanked v08i for this post:

    Rihoj (05-29-2009)

  • #3
    New Coder
    Join Date
    May 2009
    Posts
    58
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Thank you greatly, I have edited it, to count up instead of down, and it is working now. Thank you.

    for everyone else here is the code:
    Code:
    <script type='text/javascript'>
    var timer = {
    	minutes :0,
    	seconds : 0,
    	elm :null,
    	samay : null,
    	sep : ':',
    	init : function(m,s,elm)
    	{
    		m = parseInt(m,10);
    		s = parseInt(s,10);
    		if(m < 0 || s <0 || isNaN(m) || isNaN(s)) { alert('Invalid Values'); return; }
    		this.minutes = m;
    		this.seconds = s;
    		this.elm = document.getElementById(elm);
    		timer.start();
    	},
    	start : function()
    	{
    		this.samay = setInterval((this.doCountDown),1000);
    	},
    	doCountDown : function()
    	{
    		if(timer.seconds == 59)
    		{
    				timer.seconds=0;
    				timer.minutes++;
    		}else{
    			timer.seconds++;
    		}
    		timer.updateTimer(timer.minutes,timer.seconds);
    	},
    	updateTimer :  function(min,secs)
    	{
    		min = (min < 10 ? '0'+min : min);
    		secs = (secs < 10 ? '0'+secs : secs);
    		(this.elm).value = min+(this.sep)+secs;
    	},
    }
    function timerComplete()
    {
    	alert('time out buddy!!!');
    }
    window.onload = init;
    function init()
    {
        timer.init(0,0,'container');
    }
    </script>
    <div id='container'>
    
    </div>

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,146
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Quote Originally Posted by Rihoj View Post
    Thank you greatly, I have edited it, to count up instead of down, and it is working now. Thank you.
    Not for me it isn't.

  • #5
    New Coder
    Join Date
    Nov 2008
    Location
    New Delhi,India
    Posts
    26
    Thanks
    1
    Thanked 3 Times in 3 Posts

    Smile Typo found

    above code works on firefox but not in IE.
    To fix this remove the comma after the updateTimer function.

    Currently it looks like this:

    Code:
    updateTimer :  function(min,secs)
    	{
    		min = (min < 10 ? '0'+min : min);
    		secs = (secs < 10 ? '0'+secs : secs);
    		(this.elm).innerHTML = min+(this.sep)+secs;
    	},
    After removing the comma:
    Code:
    updateTimer :  function(min,secs)
    	{
    		min = (min < 10 ? '0'+min : min);
    		secs = (secs < 10 ? '0'+secs : secs);
    		(this.elm).innerHTML = min+(this.sep)+secs;
    	}
    That was a typo by me. However, if you click the link for demo it will work on all browsers.
    Will update the blog post too.
    Cheers!!!

  • #6
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,146
    Thanks
    203
    Thanked 2,547 Times in 2,525 Posts
    Nope. Does not work for me in IE6.

    Try someting easier:-

    Code:
    <script type = "text/javascript">
    
    var timeInSecs;
    var ticker;
    
    function startTimer(secs) {
    timeInSecs = parseInt(secs);
    ticker = setInterval("tick()",1000); 
    }
    
    function tick(){
    var secs = timeInSecs;
    timeInSecs++;
    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("countup").innerHTML = result;
    
    if (secs>=10) {  // 10 seconds or whatever
    window.clearInterval(ticker);
    alert ("Time's up!");
    }
    
    }
    
    startTimer(0);
    
    </script>
    
    <span id="countup" style="font-weight: bold;"></span>
    Quizmaster: In Roman Catholicism, baptism, confirmation and matrimony are three of the seven what?
    Contestant: Deadly sins.
    Last edited by Philip M; 05-29-2009 at 08:44 AM.

  • Users who have thanked Philip M for this post:

    Rihoj (05-29-2009)

  • #7
    New Coder
    Join Date
    May 2009
    Posts
    58
    Thanks
    8
    Thanked 4 Times in 4 Posts
    Wow, thank you, I did not even think of cross checking all the browsers (Stupid me...) I even downloaded FF, Safari, and IE so I could. But yes that works on all my browsers now, and it also works on all of the Safari development browsers. Thank you greatly once again.


  •  

    Posting Permissions

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