• Home
  • About
    • javajaket's Blog photo

      javajaket's Blog

      javajaket's 개발공부로그

    • Learn More
    • Email
    • Facebook
    • Instagram
    • Github
  • Posts
    • All Posts
    • CSS&HTML
    • javaScript
    • Front-end
    • Algorithm
    • etc
    • All Tags

[Codility] stop watch

29 Nov 2018

Reading time ~1 minute

스톱워치

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>stop watch</title>
  <script>
    window.onload = function() {
      let startButton = document.getElementById('start')
      let stopButton = document.getElementById('stop')
      let display = document.getElementById('display')
      let startTime,timer

      startButton.onclick = start;
      function start() {
        startButton.onclick = null
        stopButton.onclick = stop
        startTime = new Date()
        timer = setInterval(function() {
          let now = new Date()
          display.innerHTML = ((now - startTime)/1000).toFixed(2)
        },10)    
      }
      function stop() {
        clearInterval(timer);
        startButton.onclick = start;
      }
    };
  </script>
</head>
<body>
  <p id="display">0.00</p>
  <input type="button" value="start" id="start">
  <input type="button" value="stop" id="stop">
</body>
</html>


AlgorithmCodilitystop watch Share Tweet +1