Javascript Count up/down script to show the time since logged in
I've been googling but could not get the correct script. Maybe someone can point me in the correct direction?
I need to show the time a user has been active on the site since accessing a certain page. I am busy with a testing site and the user has for example 1 hour to complete a test so I need to show the time the user has been busy on the test.
I've tried 开发者_JAVA百科this site: http://javascriptkit.com/script/cut9.shtml but since I don't know javascript I don't know how to set the time to the time the person has accessed the page.
The basic code for this would probably be something like this:
var pageVisisted = new Date(); // you should probably store this in a database/attach it to the session or AT LEAST in a cookie
setInterval(function() {
var timeOnSite = new Date() - pageVisisted;
var secondsTotal = timeOnSite / 1000;
var hours = Math.floor(secondsTotal / 3600);
var minutes = Math.floor(secondsTotal / 60) % 3600;
var seconds = Math.floor(secondsTotal) % 60;
document.getElementById('counter').innerHTML = hours + ":" + minutes + ":" + seconds;
}, 1000);
Fiddle: http://jsfiddle.net/yRsBw/1/
However in a real world application you will want to store the time the user first started the test on the server and send the remaining time back to the client which you can then count down.
精彩评论