Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

stopwatch with javascript

var x;
var startstop = 0;

function startStop() { /* Toggle StartStop */

  startstop = startstop + 1;

  if (startstop === 1) {
    start();
    document.getElementById("start").innerHTML = "Stop";
  } else if (startstop === 2) {
    document.getElementById("start").innerHTML = "Start";
    startstop = 0;
    stop();
  }

}


function start() {
  x = setInterval(timer, 10);
} /* Start */

function stop() {
  clearInterval(x);
} /* Stop */

var milisec = 0;
var sec = 0; /* holds incrementing value */
var min = 0;
var hour = 0;

/* Contains and outputs returned value of  function checkTime */

var miliSecOut = 0;
var secOut = 0;
var minOut = 0;
var hourOut = 0;

/* Output variable End */


function timer() {
  /* Main Timer */


  miliSecOut = checkTime(milisec);
  secOut = checkTime(sec);
  minOut = checkTime(min);
  hourOut = checkTime(hour);

  milisec = ++milisec;

  if (milisec === 100) {
    milisec = 0;
    sec = ++sec;
  }

  if (sec == 60) {
    min = ++min;
    sec = 0;
  }

  if (min == 60) {
    min = 0;
    hour = ++hour;

  }


  document.getElementById("milisec").innerHTML = miliSecOut;
  document.getElementById("sec").innerHTML = secOut;
  document.getElementById("min").innerHTML = minOut;
  document.getElementById("hour").innerHTML = hourOut;

}


/* Adds 0 when value is <10 */


function checkTime(i) {
  if (i < 10) {
    i = "0" + i;
  }
  return i;
}

function reset() {


  /*Reset*/

  milisec = 0;
  sec = 0;
  min = 0
  hour = 0;

  document.getElementById("milisec").innerHTML = "00";
  document.getElementById("sec").innerHTML = "00";
  document.getElementById("min").innerHTML = "00";
  document.getElementById("hour").innerHTML = "00";

}
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript console.table 
Javascript :: async await iife 
Javascript :: javascript date double digit month 
Javascript :: Javascript add leading zeroes to date 
Javascript :: current date in mongodb 
Javascript :: jquery ui timepicker 
Javascript :: react onchange handler 
Javascript :: run javascript in html 
Javascript :: delete element javascript 
Javascript :: generate and download xml from javascript 
Javascript :: Regex Chords 
Javascript :: how to get current template in vuejs 
Javascript :: angular implementing Validator 
Javascript :: change url angular 
Javascript :: looping through local storage javascript 
Javascript :: Material-ui add alarm icon 
Javascript :: js initialize array with values 
Javascript :: for loop array 
Javascript :: React Native typescript start new project 
Javascript :: react hide element 
Javascript :: javascript export to pdf 
Javascript :: vuex getters 
Javascript :: three.js cube 
Javascript :: array as json 
Javascript :: app.js 
Javascript :: change cwd node 
Javascript :: javascript como recorrer un array multidimensional 
Javascript :: node assert 
Javascript :: How to find unique values from an array in sorted order js 
Javascript :: Date object for local time and date 
ADD CONTENT
Topic
Content
Source link
Name
6+4 =