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 7844759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:59:38+00:00 2026-06-02T16:59:38+00:00

Try to make a timer like following way: HTML: <div id=timer> <span id=minute class=dis>00</span>

  • 0

Try to make a timer like following way:

HTML:

<div id="timer">
     <span id="minute" class="dis">00</span> :
     <span id="second" class="dis">00</span> :
     <span id="milisecond" class="dis">00</span>
 </div>

 <input type="button" value="Start/Stop" id="startStop" />
 <input type="button" value="Reset" id="reset" />

jQuery:

 var a = false,
         t = null,
         ms = 0,
         s = 0,
         m = 0,
         dl = 10,
         now = null,
         before = new Date(),
         El = function(id) { return document.getElementById(id);};

     function dsp() {
         if(ms++ == 99){
              ms = 0;
             if(s++ == 59) {
                 s = 0;
                 m++;
             } else s = s;
         } else ms = ms;    
         El('milisecond').innerHTML = ms
         El('second').innerHTML = s < 10 ? '0' + s : s;
         El('minute').innerHTML = m < 10 ? '0' + m : m;
     }

     function r() {
         a = true;
         var els = document.getElementsByClassName('dis');
         ms = s = m = 0;
         sw();        
         for(var i in els)  {
             els[i].innerHTML = '00';    
         }
     }

     function sw() {
         a ? clearInterval(t) : t = setInterval(dsp, dl);
         a = !a;
     }
     El('startStop').addEventListener('click', sw, true);
     El('reset').addEventListener('click', r, true);

It works just fine. But problem is that it stop execution when window switch or tab change happen. Before submit this question I read this thread, but fail to implement it in my snippet.

Please help me with some solution or suggestion..

Here is the fiddle

  • 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-02T16:59:40+00:00Added an answer on June 2, 2026 at 4:59 pm

    Capture the start time (see Marc’s comment) and keep track of previous runs (offset):

    var a = false;
    var dl = 10;
    var El = function(id) { return document.getElementById(id);};
    var start = null; // starting time of current measurement
    var offset = 0;   // offset for previous measurement
    
    function dsp() {
        var tmp = new Date(new Date() - start + offset ); // calculate the time
        var ms = tmp.getMilliseconds();
        var m = tmp.getMinutes();
        var s = tmp.getSeconds();
        El('milisecond').innerHTML = ms;
        El('second').innerHTML = s < 10 ? '0' + s : s;
        El('minute').innerHTML = m < 10 ? '0' + m : m;
    }
    
    function r() {
        a = true;
        var els = document.getElementsByClassName('dis');    
        sw();
        offset = 0; // Clear the offset
        for(var i in els)  {
            els[i].innerHTML = '00';    
        }
    }
    
    function sw() {
        if(a){
        // If the timer stops save the current value
            offset += (new Date()) - start;
        }else
            start = new Date();
        a ? clearInterval(t) : t = setInterval(dsp, dl);
        a = !a;
    }
    document.getElementById('startStop').addEventListener('click', sw, true);
    document.getElementById('reset').addEventListener('click', r, true);
    

    JSFiddle

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

Sidebar

Related Questions

I try to make a registration form. When user post the fields, i check
I try to make an animation with an image in my view but it
I try to make Ext.Data.Store working... With a normal proxy it works great: var
I try to make an Android application which communicates with a non-standard ISO15693 (NFC-V)
I try to make have an EventListener in ItemRenderer but its not working. How
I try to make a displaying list with onmouseover() with some links but they
I try to make a RegEx for validating a form in javascript. The RegEx
I will try to make this as clear as I can, but if you
I'm looking to try to make an efficient delete method to work on a
I am starting this question to try and make a central point developers can

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.