Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8044505
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:15:05+00:00 2026-06-05T05:15:05+00:00

I asked yesterday about saving a timer value when the browser closes and then

  • 0

I asked yesterday about saving a timer value when the browser closes and then start counting again when the user opens it. I’ve found that using cookies must be a good solution, so i’ve added the set and getcookie functions, but still i can’t get my timer values. This might be easy, but i cant see what’s wrong because i’m still too noob in javascript.
Does someone know what i’m doing wrong?
thank you!!
here’s the code i have so far:

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"><head>
    <title>Test</title>
    <script type="text/javascript">
var sec = 0;
var min =  0;
var hr = 0;
var dias = 0;
var bool = true;
function stopwatch() {
        sec++;
        if (sec == 60) {
            sec = 0;
            min += 1;
        }

        if (min == 60) {
            min = 0;
            hr += 1;
        }

        if (hr == 24) {
            hr = 0;
            dias += 1;
        }

        totalTime = ((dias<=9) ? "0" + dias : dias) + "d, " + ((hr<=9) ? "0" + hr : hr) + " : " + ((min<=9) ? "0" + min : min) + " : " + ((sec<=9) ? "0" + sec : sec);
        document.getElementById("timer").innerHTML = totalTime;
        if (bool == true) {
        start = setTimeout("stopwatch()", 1000);

        }
    }

function setCookie(name, value, expires) {
document.cookie = name + "=" + escape(value) + "; path=/" + ((expires == null) ? "" : "; expires=" + expires.toGMTString());
}



function getCookie (name) {
    var cname = name + "=";               
    var dc = document.cookie;

    if (dc.length > 0) {              
        begin = dc.indexOf(cname);       
            if (begin != -1) {           
            begin += cname.length;       
            end = dc.indexOf(";", begin);
                if (end == -1) end = dc.length;
                return unescape(dc.substring(begin, end));
            } 
        }
    return null;
}


var exp = new Date();                                  
exp.setTime(exp.getTime() + (1000 * 60 * 60 * 24 * 30));

    </script>
</head>
<body onload="stopwatch()">
    <div id="timer" name="timer"> </div>
  <button onclick="bool = false"; > pause </button>
  <button onclick="bool = true;stopwatch();" > resume </button>

    <form>
      <input type="button" value="Set a Cookie" onClick="setCookie('myCookie',timer.value, exp)">
    </form>
<form>
<input type="button" value="Get Cookie Value" onClick="this.form.tf.value = getCookie('myCookie')">
<input type="text" name="tf" size="30">
</form>

 </body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T05:15:06+00:00Added an answer on June 5, 2026 at 5:15 am

    Firstly, few issues with your code:

    • Strings shouldn’t be used in setTimeouts
    • Your variables should be initialised as integers, not strings.

    Back to your problem, use the unload event to save a cookie with the current time when the user closes the page. Then when the user opens the page again, detect the cookie and continue from where you left off.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I asked yesterday about saving a timer value when the browser closes and then
I asked a question about different testing frameworks yesterday. This question can be found
Yesterday my friend asked me a question about this query: select * from user
Yesterday I asked about serving byte ranges from PHP. Today my question is -
Yesterday I asked about using an exlusive maven repository, now it turns I need
Yesterday I asked this general question about decimals and their internal precisions. Here is
I asked a question about bigints yesterday which was kindly answered. However, I have
I asked a question about making a greatest n per group type query yesterday
I asked a question yesterday about password safety... I am new at security... I
I asked this question yesterday about storing a plot within an object. I tried

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.