SHARE
TWEET

cas

alatoru Jun 2nd, 2020 (edited) 779 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. var h1 = document.getElementsByTagName('h1')[0],
  2.     start = document.getElementById('start'),
  3.     stop = document.getElementById('stop'),
  4.     clear = document.getElementById('clear'),
  5.     seconds = 0, minutes = 0, hours = 0,
  6.     t;
  7.  
  8. function add() {
  9.     seconds++;
  10.     if (seconds >= 60) {
  11.         seconds = 0;
  12.         minutes++;
  13.         }
  14.         if (minutes >= 60) {
  15.             minutes = 0;
  16.             hours++;
  17.         }
  18.  
  19.  
  20.     h1.textContent = (hours ? (hours > 9 ? hours : "0" + hours) : "00") + ":" + (minutes ? (minutes > 9 ? minutes : "0" + minutes) : "00") + ":" + (seconds > 9 ? seconds : "0" + seconds);
  21.  
  22.     timer();
  23. }
  24. function timer() {
  25.     t = setTimeout(add, 1000);
  26. }
  27. //timer();
  28.  
  29.  
  30. /* Start button */
  31. start.onclick = function(){
  32.     start.disabled = true;
  33.     timer();
  34. };
  35.  
  36. /* Stop button */
  37. stop.onclick = function() {
  38.     clearTimeout(t);
  39.     start.disabled = false;
  40. }
  41.  
  42. /* Clear button */
  43. clear.onclick = function() {
  44.     h1.textContent = "00:00:00";
  45.     seconds = 0; minutes = 0; hours = 0;
  46. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top