Home javascript Stopwatch in js

Stopwatch in js

Author

Date

Category

Could you please tell me if there is a stopwatch js code:

& lt; html & gt;
  & lt; head & gt;
    & lt; meta http-equiv = "Content-Type" content = "text / html; charset = UTF-8" & gt;
    & lt; title & gt; Timer & lt; / title & gt;
    & lt; script type = "text / javascript" & gt;
      function init ()
      {
        sec = 0;
        setInterval (tick, 1000);
      }
      function tick ()
      {
        seC++;
        document.getElementById ("timer").
          childNodes [0] .nodeValue = sec;
      }
    & lt; / script & gt;
  & lt; / head & gt;
  & lt; body onload = "init ()" & gt;
    & lt; div id = "timer" & gt; 0 & lt; / div & gt;
  & lt; / body & gt;
& lt; / html & gt; 

Answer 1, authority 100%

As one of the variations, a regular stopwatch:

window.onload = () = & gt; {
 StartStop ();
}
// declare variables
var base = 60;
var clocktimer, dateObj, dh, dm, ds, ms;
var readout = '';
var h = 1,
 m = 1,
 tm = 1,
 s = 0,
 ts = 0,
 ms = 0,
 init = 0;
// function to clear the field
function ClearWITHlock () {
 clearTimeout (clocktimer);
 h = 1;
 m = 1;
 tm = 1;
 s = 0;
 ts = 0;
 ms = 0;
 init = 0;
 readout = '00: 00: 00 ';
 document.MyForm.stopwatch.value = readout;
}
// function to start the stopwatch
function StartTIME () {
 var cdateObj = new Date ();
var t = (cdateObj.getTime () - dateObj.getTime ()) - (s * 1000);
 if (t & gt; 999) {
  s ++;
 }
 if (s & gt; = (m * base)) {
  ts = 0;
  m ++;
 } else {
  ts = parseInt ((ms / 100) + s);
  if (ts & gt; = base) {
   ts = ts - ((m - 1) * base);
  }
 }
 if (m & gt; (h * base)) {
  tm = 1;
  h ++;
 } else {
  tm = parseInt ((ms / 100) + m);
  if (tm & gt; = base) {
   tm = tm - ((h - 1) * base);
  }
 }
 ms = Math.round (t / 10);
 if (ms & gt; 99) {
  ms = 0;
 }
 if (ms == 0) {
  ms = '00';
 }
 if (ms & gt; 0 & amp; & amp; ms & lt; = 9) {
  ms = '0' + ms;
 }
 if (ts & gt; 0) {
  ds = ts;
  if (ts & lt; 10) {
   ds = '0' + ts;
  }
 } else {
  ds = '00';
 }
 dm = tm - 1;
 if (dm & gt; 0) {
  if (dm & lt; 10) {
   dm = '0' + dm;
  }
 } else {
  dm = '00';
 }
 dh = h - 1;
 if (dh & gt; 0) {
  if (dh & lt; 10) {
   dh = '0' + dh;
  }
 } else {
  dh = '00';
 }
 readout = dh + ':' + dm + ':' + ds;
 document.MyForm.stopwatch.value = readout;
 clocktimer = setTimeout ("StartTIME ()", 1);
}
// Start and stop function
function StartStop () {
 if (init == 0) {
  ClearWITHlock ();
  dateObj = new Date ();
  StartTIME ();
  init = 1;
 } else {
  clearTimeout (clocktimer);
  init = 0;
 }
} 
& lt; form name = MyForm & gt;
 & lt; input name = stopwatch size = 10 value = "00: 00: 00.00" disabled & gt;
& lt; / form & gt; 

Answer 2, authority 40%

// Stopwatch
// initial variables
min = 0;
hour = 0;
// Leave your function
function init () {
  sec = 0;
  setInterval (tick, 1000);
}
// The main function is tick ()
function tick () {
  seC++;
if (sec & gt; = 60) {// set numeric parameters that change during the program
    min ++;
    sec = sec - 60;
  }
  if (min & gt; = 60) {
    hour ++;
    min = min - 60;
  }
  if (sec & lt; 10) {// Visual design
    if (min & lt; 10) {
      if (hour & lt; 10) {
        document.getElementById ('timer'). innerHTML = '0' + hour + ': 0' + min + ': 0' + sec;
      } else {
        document.getElementById ('timer'). innerHTML = hour + ': 0' + min + ': 0' + sec;
      }
    } else {
      if (hour & lt; 10) {
        document.getElementById ('timer'). innerHTML = '0' + hour + ':' + min + ': 0' + sec;
      } else {
        document.getElementById ('timer'). innerHTML = hour + ':' + min + ': 0' + sec;
      }
    }
  } else {
    if (min & lt; 10) {
      if (hour & lt; 10) {
        document.getElementById ('timer'). innerHTML = '0' + hour + ': 0' + min + ':' + sec;
      } else {
        document.getElementById ('timer'). innerHTML = hour + ': 0' + min + ':' + sec;
      }
    } else {
      if (hour & lt; 10) {
        document.getElementById ('timer'). innerHTML = '0' + hour + ':' + min + ':' + sec;
      } else {
        document.getElementById ('timer'). innerHTML = hour + ':' + min + ':' + sec;
      }
    }
  }
}

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions