Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 05-29-2009, 05:59 AM   PM User | #1
Rihoj
New Coder

 
Join Date: May 2009
Posts: 58
Thanks: 8
Thanked 4 Times in 4 Posts
Rihoj is an unknown quantity at this point
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..
Rihoj is offline   Reply With Quote
Old 05-29-2009, 06:31 AM   PM User | #2
v08i
New Coder

 
Join Date: Nov 2008
Location: New Delhi,India
Posts: 26
Thanks: 1
Thanked 3 Times in 3 Posts
v08i is an unknown quantity at this point
Smile Countdown timer

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

Please let us know.
v08i is offline   Reply With Quote
Users who have thanked v08i for this post:
Rihoj (05-29-2009)
Old 05-29-2009, 07:00 AM   PM User | #3
Rihoj
New Coder

 
Join Date: May 2009
Posts: 58
Thanks: 8
Thanked 4 Times in 4 Posts
Rihoj is an unknown quantity at this point
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>
Rihoj is offline   Reply With Quote
Old 05-29-2009, 07:27 AM   PM User | #4
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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.
Philip M is offline   Reply With Quote
Old 05-29-2009, 07:49 AM   PM User | #5
v08i
New Coder

 
Join Date: Nov 2008
Location: New Delhi,India
Posts: 26
Thanks: 1
Thanked 3 Times in 3 Posts
v08i is an unknown quantity at this point
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!!!
v08i is offline   Reply With Quote
Old 05-29-2009, 08:29 AM   PM User | #6
Philip M
Supreme Master coder!

 
Philip M's Avatar
 
Join Date: Jun 2002
Location: London, England
Posts: 17,044
Thanks: 197
Thanked 2,412 Times in 2,390 Posts
Philip M has a spectacular aura aboutPhilip M has a spectacular aura aboutPhilip M has a spectacular aura about
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..
Philip M is offline   Reply With Quote
Users who have thanked Philip M for this post:
Rihoj (05-29-2009)
Old 05-29-2009, 01:53 PM   PM User | #7
Rihoj
New Coder

 
Join Date: May 2009
Posts: 58
Thanks: 8
Thanked 4 Times in 4 Posts
Rihoj is an unknown quantity at this point
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.
Rihoj is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:25 PM.


Advertisement
Log in to turn off these ads.