PDA

View Full Version : Minute Counter in Javascript



robson_jerome
January 21st, 2009, 05:02 PM
Hi. I'm doing a website called wastedminutes.com and I would like a counter at the top telling the user how many minutes they've wasted on the website.
Can anyone give me some Javascript code with which to achieve this?
Preferably it would keep a running counter without having to refresh the page.
Is javascript the right thing to be using?

Thanks

borrob
January 22nd, 2009, 07:49 AM
here is an example



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns ="http://www.w3.org/1999/xhtml">

<head>
<script language="JavaScript">
<!--
var wasted_minutes = 0;
var interval_period = 6000;

onload = function()
{
interval_pointer = setInterval( timer_handler , interval_period );
}

function timer_handler()
{
clearInterval( interval_pointer );
var text = '';
wasted_minutes++;
if( wasted_minutes == 1 )
{
text = " you wasted " + wasted_minutes + " minute on my site.";
}
else
{
text = " you wasted " + wasted_minutes + " minutes on my site.";
}
document.getElementById( 'time_diplay' ).innerHTML = text;
interval_pointer = setInterval( timer_handler , interval_period );
}
//-->
</script>
<noscript></noscript>
</head>

<body bgcolor='#ffffff'>
<div id='time_diplay' >
you wasted 0 minutes on my site.
</div>

</body>
</html>

simplistik
January 22nd, 2009, 09:18 AM
<script language="JavaScript">

eeew

robson_jerome
January 22nd, 2009, 05:21 PM
here is an example



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns ="http://www.w3.org/1999/xhtml">

<head>
<script language="JavaScript">
<!--
var wasted_minutes = 0;
var interval_period = 6000;

onload = function()
{
interval_pointer = setInterval( timer_handler , interval_period );
}

function timer_handler()
{
clearInterval( interval_pointer );
var text = '';
wasted_minutes++;
if( wasted_minutes == 1 )
{
text = " you wasted " + wasted_minutes + " minute on my site.";
}
else
{
text = " you wasted " + wasted_minutes + " minutes on my site.";
}
document.getElementById( 'time_diplay' ).innerHTML = text;
interval_pointer = setInterval( timer_handler , interval_period );
}
//-->
</script>
<noscript></noscript>
</head>

<body bgcolor='#ffffff'>
<div id='time_diplay' >
you wasted 0 minutes on my site.
</div>

</body>
</html>


Thanks, that's great, but it's going too fast...
The minutes are going up every 10 seconds or so.

robson_jerome
January 22nd, 2009, 05:29 PM
tis OK.
I've sorted it. It needed to be 60000.
Thanks very much for your help