Javascript Stopwatch - Ash Princess Kawaii
Breaking News
Loading...
14 Oktober 2014

Javascript Stopwatch

Oktober 14, 2014
Javascript-Stopwatch

A simple Javascript stopwatch which allows you to count up from zero with start, pause, resume and stop controls.

CSS
<style>
body {
margin:0 auto;
padding:0px 0px 0px 0px;
background: url(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjpjDpyJbVCi4yorgsEnKeWZ9ADXMzfsngyVWRvFGEgXhfg2MW_qexkE-pRZ7yMYXC5CikliotgQO763K0PJRKwVnMPR7lkaolvkO_Clsv2o3g5l1c0LYKs_jlbLXIHzbeOcqyETgGNPMJ0/s0/botmen2.png) repeat;
font-family:Arial,Tahoma,Century gothic, sans-serif;
color:#666;
font-size:13px;
}
img {
border:0;
margin: 0 0;
}
a {
color:#4D87A7;
text-decoration:none;
outline:none;
}
a:hover {
color:#fff;
text-decoration:none;
}
.wrapper {
  width: 800px;
  margin: 150px auto;
  color: #fff;
  text-align: center;
}
h1, h2, h3 {
  font-family: 'Roboto', sans-serif;
  font-weight: 100;
  font-size: 2.6em;
  text-transform: uppercase;
}
#seconds, #tens {
  font-size: 2em;
}
button {
  -moz-border-radius: 5px;
  -webkit-border-radius: 5px;
  border-radius: 5px;
  -khtml-border-radius: 5px;
  background: transparent;
  color: gray;
  border: solid 1px #333;
  text-decoration: none;
  cursor: pointer;
  font-size: 1.2em;
  padding: 18px 10px;
  width: 180px;
  margin: 10px;
  outline: none;
}
button:hover {
  -webkit-transition: all 0.5s ease-in-out;
  -moz-transition: all 0.5s ease-in-out;
  transition: all 0.5s ease-in-out;
  background: #222;
  border: solid 1px #333;
  color: #ddd;
}
</style>
HTML
<p><span id="seconds">00</span>:<span id="tens">00</span></p>
CONTROL
<button id="button-start">Start</button>
<button id="button-stop">Stop</button>
<button id="button-reset">Reset</button>

JAVASCRIPT
<script>
window.onload = function () {
  var seconds = 00;
  var tens = 00;
  var appendTens = document.getElementById("tens")
  var appendSeconds = document.getElementById("seconds")
  var buttonStart = document.getElementById('button-start');
  var buttonStop = document.getElementById('button-stop');
  var buttonReset = document.getElementById('button-reset');
  var Interval ;
  buttonStart.onclick = function() {
     clearInterval(Interval);
     Interval = setInterval(startTimer, 10);
  }
    buttonStop.onclick = function() {
       clearInterval(Interval);
  }
  buttonReset.onclick = function() {
     clearInterval(Interval);
    tens = "00";
   seconds = "00";
    appendTens.innerHTML = tens;
   appendSeconds.innerHTML = seconds;
  }
  function startTimer () {
    tens++;
    if(tens < 9){
      appendTens.innerHTML = "0" + tens;
    }
    if (tens > 9){
      appendTens.innerHTML = tens;
    }
    if (tens > 99) {
      console.log("seconds");
      seconds++;
      appendSeconds.innerHTML = "0" + seconds;
      tens = 0;
      appendTens.innerHTML = "0" + 0;
    }
    if (seconds > 9){
      appendSeconds.innerHTML = seconds;
    }
  }
}
</script>

See the Pen Javascript Stopwatch by Erna Ayuning Nareswari (@ashavenger) on CodePen.

1 Comments: