PDA

View Full Version : Resolved Counting up timer(either need to code, or idea of what to do.)



Rihoj
05-29-2009, 06:59 AM
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?

v08i
05-29-2009, 07:31 AM
Probably this can help http://www.vijayjoshi.org/2009/03/23/creating-a-countdown-timer-in-javascript/

Please let us know.

Rihoj
05-29-2009, 08:00 AM
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:


<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>

Philip M
05-29-2009, 08:27 AM
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.

v08i
05-29-2009, 08:49 AM
above code works on firefox but not in IE.
To fix this remove the comma after the updateTimer function.

Currently it looks like this:


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:

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!!!

Philip M
05-29-2009, 09:29 AM
Nope. Does not work for me in IE6.

Try someting easier:-



<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.

Rihoj
05-29-2009, 02:53 PM
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.