SHARE
TWEET

Untitled

a guest May 26th, 2019 60 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. (function(){
  2.   'use strict';
  3.   var timer = document.getElementById('timer');//
  4.   var min = document.getElementById('min');
  5.   var sec = document.getElementById('sec');
  6.   var start = document.getElementById('start');//
  7.   var restart = document.getElementById('restart');
  8.   var stop = document.getElementById("stop");
  9.  
  10.   var startTime;
  11.   var timeLeft;
  12.   var timeToCountDown = 0;
  13.   var timerId;
  14.   var minutes = 0;
  15.   var seconds = 0;
  16.   var isRunning = false;
  17.   //A flag recognizing whether timer's done or not
  18.   //When it's done that's false, it can work again to push "reset"
  19.   //var isDone = false;
  20.  
  21.   stop.style.display = "none";
  22.   function UpdateTimer(t){
  23.     var d = new Date(t);
  24.     var m = d.getMinutes();
  25.     var s = d.getSeconds();
  26.     var msec = d.getMilliseconds();
  27.     m = ('0' + m).slice(-2);
  28.     s = ('0' + s).slice(-2);
  29.     msec = ('00' + msec).slice(-3);
  30.     //extract the last two digits like 012->12, 02->02
  31.     //the minus value means finding from a bottom
  32.     //getting by id of html
  33.     timer.textContent = m + ':' + s + '.' + msec;
  34.     document.title = timer.textContent;
  35.   }
  36.  
  37.   //Date.now is used msec, and timeToCountDown corresponds it.
  38.   // PassedTime is calculated by decreasing CurrentTime using Date.now() from StartTime
  39.   function CountDown(){
  40.     //setTimeout(function, msec)
  41.     //after a predetermined time, the following function is performed
  42.     //in this case, it sets up updating time of this timer
  43.     timerId = setTimeout(function(){
  44.       var passedTime = Date.now() - startTime;
  45.       timeLeft = timeToCountDown - passedTime;
  46.       //console.log(timeLeft);
  47.       if(timeLeft <= 0){
  48.         clearTimeout(timerId);//to initialize the passing time by setTimeout(,)
  49.         timeLeft = 0;//In display to avoid errors, setting up timeLeft just to 0
  50.         timeToCountDown = 0;//initialization of timer
  51.         UpdateTimer(timeLeft);
  52.         start.style.display = "";
  53.         stop.style.display = "none";
  54.         isRunning = false;
  55.         //isDone = true;
  56.         return;
  57.       }
  58.       UpdateTimer(timeLeft);
  59.       CountDown();
  60.     },10);
  61.   }
  62.   start.addEventListener('click', function(){
  63.     if(isRunning === false){ //&& isDone === false){
  64.       isRunning = true;
  65.       start.style.display = "none";
  66.       stop.style.display = "";
  67.       startTime = Date.now();
  68.       CountDown();
  69.     }
  70.   });
  71.   stop.addEventListener('click', function(){
  72.     if(isRunning === true){ //&& isDone === false){
  73.       isRunning = false;
  74.       timeToCountDown = timeLeft;//Renew timeToCountDown to timeLeft
  75.       clearTimeout(timerId);
  76.       start.style.display = "";
  77.       stop.style.display = "none";
  78.     }
  79.   });
  80.   min.addEventListener('click',function(){
  81.     //while the timer is running
  82.     if(isRunning === true){
  83.       return;
  84.     } else {
  85.       //allowable operation is up to 59:00.000
  86.       if(timeToCountDown >= 59*60*1000){
  87.         return;
  88.       } else { timeToCountDown += 60 * 1000;}
  89.       UpdateTimer(timeToCountDown);
  90.     }
  91.   });
  92.   sec.addEventListener('click',function(){
  93.     if(isRunning === true){
  94.       return;
  95.     } else {
  96.       //allowable operation is up to 59:59.000
  97.       if(timeToCountDown >= 59*60*1000+59*1000){
  98.         return;
  99.       } else { timeToCountDown += 1000;}
  100.       UpdateTimer(timeToCountDown);
  101.     }
  102.   });
  103.   reset.addEventListener('click',function(){
  104.     isRunning = false;
  105.     //to avoid interfering isDone=true within CountDown function
  106.     //setTimeout(function(){ isDone = false;},10);
  107.     timeToCountDown = 0;
  108.     UpdateTimer(timeToCountDown);
  109.   });
  110.  
  111. })();
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