Page 1 of 1

Remember where you are in time.

Posted: Mon Mar 08, 2010 1:53 pm
by Goofan
Hi I got a Timer which will count down for something. But my problem is that the user can refresh the page or go to another paggge and then
return 5 min later and the timer will restart. So i were wondering if there is some kind of way to make it input the last recorded data into a
database so that the user returns 5 min later and (will have date/time function to see how long) it will begin counting but minus 5 min and
the allready passed time.


My code for timer is:

Code: Select all

 
var sec=59;
var timer_is_on=0;
var Reset=0;
var Min=11;
var Hour=1;
var Day=1;
 
function SetTime(){
 
    if (Reset == 0){
    if (sec==0 && Min==0 && Hour==0){
        Hour=24;
        Min=59;
        sec=59;
        RedoTimer();
        Reset=1;
    }
    }
}
 
function timedCount(){
 
    if (sec>=0){
        if (sec<10 && Min>=10){
        document.getElementById('txt').value=Day + " : " + Hour + " : " + Min + " : " + "0" + sec;
        
        }else if (Min<10 && sec>=10){
        document.getElementById('txt').value=Day + " : " + Hour + " : " + "0" + Min + " : " + sec;
        
        }else if (Min<10 && sec<10){
        document.getElementById('txt').value=Day + " : " + Hour + " : " + "0" + Min + " : " + "0" + sec;
        
        }else{
        document.getElementById('txt').value=Day + " : " + Hour + " : " + Min + " : " + sec;
        }
        
    Timer();
    }
    
    if (sec==-1 && Min>0){
    MinCheck();
    }
    
    if (sec==-1 && Min==0 && Hour>0){
    HourCheck();
    }
    
    if (sec==-1 && Min==0 && Hour==0 && Day>0){
    DayCheck();
    }
    
    if (sec==0 && Min==0 && Hour==0){
    StopCount();
    }
    
    if (Reset==1 && sec==0){
    ReReset();
    }
}
 
function Timer(){
    sec=sec-1;
    setTimeout("timedCount()",1000);
}
 
function MinCheck(){
 
    Min=Min-1;
    sec=59;
}
 
function HourCheck(){
 
    Hour=Hour-1;
    Min=Min-1;
    Min=59;
    sec=59;
}
 
function DayCheck(){
    Day=Day-1;
    Hour=Hour-1;
    Min=Min-1;
    Hour=1;
    Min=59;
    sec=59;
}
 
function ReReset(){
 
    Reset=0;
}
 
function doTimer(){
 
    if (Reset==0){
        if (timer_is_on=1){
            timedCount();
        }
    }
}
 
function RedoTimer(){
timer_is_on=1;
timedCount();
}
 
function stopCount(){
timer_is_on=0;
}
 
To start the timer they press a button on this page:

Code: Select all

 
<html>
<head>
<script type="text/javascript" src="timer.js">
</script>
</head>
<body>
<form><input type="button" value="Start Timer!" onclick="doTimer()" /> <br>
<input type="text" id="txt" /> <br>
<input type="button" value="Restart Timer!" onclick="SetTime()" /></form> <br>
</body>
</html>
 
 
 

Re: Remember where you are in time.

Posted: Mon Mar 08, 2010 2:03 pm
by AbraCadaver
Not sure if this is the best way, but the only one that comes to me is use a cookie. Every time you decrement the counter, update the cookie with that time. Then on page load, check for the cookie and subtract that time from the current time.

Re: Remember where you are in time.

Posted: Mon Mar 08, 2010 2:14 pm
by Goofan
yea well that wount work if the user got no save of cookies... =( thought there might be a way to convert the js variable to php and from there save it to a database. But if that is possible (how if so?) then i need a way to convert a variable back later.

Re: Remember where you are in time.

Posted: Mon Mar 08, 2010 2:18 pm
by AbraCadaver
Goofan wrote:yea well that wount work if the user got no save of cookies... =( thought there might be a way to convert the js variable to php and from there save it to a database. But if that is possible (how if so?) then i need a way to convert a variable back later.
AJAX. Use xmlhhtprequest to send the time and userid etc to a PHP file that would insert it in the DB.