Aniruddha Bhondwe
Aniruddha Bhondwe

Reputation: 831

Javascript Timer should go on after Web Browser is closed

I have written the following code for a Javascript timer with a few validations.

function timedcounter(){
    time=time+1;
    t = setTimeout(function(){ timedcounter() }, 1000);
    display(time);
}

function starttimer() {
    var autotask=document.getElementById("Tasks").value

    if(autotask=="") {
        $("#errormsg2").show();
    } else {
        if(!timer_is_on){
            add_row_flag=1;
            var value=document.getElementById("startstopbutton").value
            if(value=="Start") {
                timer_is_on=1;
                value="Stop";
                document.getElementById("startstopbutton").innerHTML="Stop"
                $("#errormsg2").hide();
                timedcounter();
            }
        } else {
            add_row_flag=0;
            stoptimer();
        }
    }
}



function stoptimer(){
    clearTimeout(t);
    timer_is_on=0;
    document.getElementById("startstopbutton").innerHTML="Start"
    document.getElementById("disptime").value=null;

    finaltime(time);
    time=-1;
}

I wanted to know is there any way to keep the timer going even after the user closes the browser, as in when he reopens the webpage timer should show the time elapsed since he started the timer.

Upvotes: 0

Views: 613

Answers (1)

jakee
jakee

Reputation: 18556

You have three options:

  1. Store the timer start time with timezone in localStorage and on page reopen read it and continue where you left off
  2. Use a web server and store user timer start times there
  3. Store it in a cookie

Upvotes: 1

Related Questions