Advertisement
Guest User

Untitled

a guest
May 22nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. window.onload = function () {
  2.        
  3.         var checkmark = 0,
  4.             /* these 2 variables are used when we need to get the time displaying on-screen, for instance, when the timer is paused and we click 'play' again.
  5.             */
  6.             minutes = 0,
  7.             seconds = 0,
  8.             timerId = 0,
  9.             /* initiates the variables with a standard pomodoro and standard breaks. These variables are used to "save" the settings.
  10.             */
  11.             pomodoroMinutes = "25",
  12.             pomodoroSeconds = "00",
  13.             shortBreakMinutes = "5",
  14.             shortBreakSeconds = "00",
  15.             longBreakMinutes = "15",
  16.             longBreakSeconds = "00",
  17.            
  18.            
  19.             ticking = false,
  20.             fadeAudio,
  21.             sound,
  22.             firstTimeButtonClick = true,
  23.             lastIntervalWasAPomodoro = true;      
  24.      
  25.         var p = document.getElementById("timerButton");          
  26.            
  27.            
  28.             /*Avoid setInterval; use setTimeout instead. http://zetafleet.com/blog/why-i-consider-setinterval-harmful (requestAnimationFrame may be another alternative)
  29.             https://gist.github.com/jkantr/22880fd3bdcdcd741c4f25fb3d379d35*/
  30.        
  31.         p.onclick = timerButtonClicked;    
  32.        
  33.        
  34.        
  35.         function timerButtonClicked(event) {            
  36.            
  37.            
  38.             if (firstTimeButtonClick) { //if starting the timer for the first time
  39.                
  40.                 firstTimeButtonClick = false;
  41.             }
  42.                
  43.            
  44.             if (ticking) { //if timer is already ticking: ticking = true
  45.                
  46.                 console.log("if ticking true");
  47.                  
  48.               /*  var danger = document.getElementsByClassName('btn-danger');
  49.                 console.log(danger[0]);
  50.                 danger[0].className = 'btn-success';*/
  51.                
  52.                 document.getElementById("timerButton").classList.remove("btn-warning");
  53.                 document.getElementById("timerButton").classList.add("btn-success");
  54.                
  55.                 document.getElementById("fa").classList.remove("fa-pause");
  56.                 document.getElementById("fa").classList.add("fa-play");  
  57.                
  58.                
  59.                 text = document.getElementById('buttontext');
  60.                 text.innerHTML = 'Play';
  61.                
  62.                 ticking = false;            
  63.                 clearInterval(timerId);  
  64.                 sound.pause();
  65.                
  66.                
  67.                
  68.                
  69.             } else { //if timer is stopped: ticking = false          
  70.                
  71.                
  72.                 playTickingSound();        
  73.                
  74.                 document.getElementById("timerButton").classList.remove("btn-success");
  75.                 document.getElementById("timerButton").classList.add("btn-warning");
  76.                
  77.                 document.getElementById("fa").classList.remove("fa-play");
  78.                 document.getElementById("fa").classList.add("fa-pause");              
  79.                
  80.                 text = document.getElementById('buttontext');
  81.                 text.innerHTML = 'Pause';
  82.                
  83.                 ticking = true;
  84.                 seconds = document.getElementById("seconds").innerHTML;
  85.                 minutes = document.getElementById("minutes").innerHTML;
  86.            
  87.                 timerId = setInterval(decrementTimer, 1000);
  88.                
  89.             }        
  90.            
  91.         }
  92.        
  93.         function playTickingSound() {
  94.            
  95.             sound = new Audio('ticking.wav');
  96.             sound.play();        
  97.  
  98.             fadeAudio = setInterval(function () {                    
  99.  
  100.  
  101.                 // Set the point in playback that fadeout begins.                
  102.                 var fadePoint = sound.duration - 9;  //=2      
  103.  
  104.  
  105.                 // Only fade if past the fade out point or not at zero already
  106.                 if ((sound.currentTime >= fadePoint) && (sound.volume != 0.0)) {                    
  107.                    
  108.                     if(sound.volume <= 0.12) {                        
  109.                         sound.volume = 0;
  110.                         clearInterval(fadeAudio);
  111.                     } else {                    
  112.                         sound.volume = sound.volume - 0.1;
  113.                     }
  114.                 }              
  115.             }, 1000);        
  116.            
  117.         }
  118.        
  119.         function decrementTimer() {            
  120.            
  121.             console.log("Entered decrementTimer()");
  122.             minutes = document.getElementById("minutes").innerHTML;
  123.             seconds = document.getElementById("seconds").innerHTML;
  124.            
  125.             if(minutes>0 || seconds>0) {        
  126.            
  127.                 if(seconds>0) {            
  128.  
  129.                     seconds = seconds - 1;
  130.                    
  131.                     if((seconds+"").length === 1){
  132.                         seconds = "0"+seconds;
  133.                     }
  134.                    
  135.                     document.getElementById("seconds").innerHTML = seconds;
  136.                    
  137.                    
  138.  
  139.                 } else { //if minutes > 0          
  140.  
  141.                     minutes = minutes - 1;
  142.                     seconds = 59;
  143.                    
  144.                     //adds a zero on the left if needed
  145.                     if((minutes+"").length === 1){
  146.                         minutes = "0"+minutes;
  147.                     }
  148.  
  149.                     document.getElementById("minutes").innerHTML = minutes;
  150.                     document.getElementById("seconds").innerHTML = seconds;
  151.  
  152.                 }
  153.             }
  154.            
  155.             if(minutes==0 && seconds==0) {
  156.              
  157.                 clearInterval(timerId); //CHECK
  158.                 zeroTimer();
  159.             }
  160.            
  161.            
  162.             //console.log("decrement timer function 2");
  163.            
  164.         }
  165.        
  166.         function zeroTimer() {
  167.            
  168.                              
  169.                
  170.                 //Everytime a pomodoro ends, add 1 to var checkmark
  171.                 if(lastIntervalWasAPomodoro === true) {
  172.                     checkmark = checkmark + 1;
  173.                 }
  174.            
  175.                 if(checkmark > 4) {
  176.                     checkmark = 0;
  177.                 }
  178.                
  179.                 console.log("checkmark", checkmark);
  180.                
  181.                 //If we're in the 3rd short break
  182.                 if(checkmark === 3) {
  183.                  
  184.                     //CHECK IF IT'S WORKING
  185.                     document.getElementById("nextBreak").innerHTML = "Next break is a: long break";
  186.                    
  187.                 }
  188.                
  189.                
  190.                 if(checkmark < 4 && lastIntervalWasAPomodoro == true ) {
  191.                     shortBreak();                    
  192.                     return;
  193.                 }
  194.            
  195.                 if((checkmark == 4) && (lastIntervalWasAPomodoro == true)) {
  196.                     longBreak();
  197.                     return;
  198.                 }
  199.            
  200.                 if(checkmark < 4 && lastIntervalWasAPomodoro == false ) {
  201.                     console.log("STARTING A POMODORO");
  202.                     spawnNotification("Back to work!", "Your break is over!");
  203.                     //alertify.alert("Your break is up! Time to go back to work!");
  204.                    
  205.                     if((lastIntervalWasAPomodoro) == false && (checkmark != 3)) {                        
  206.                         document.getElementById("nextBreak").innerHTML = "Next break is a: short break";
  207.                     }
  208.                    
  209.                     document.getElementById("minutes").innerHTML = pomodoroMinutes;
  210.                     document.getElementById("seconds").innerHTML = pomodoroSeconds;
  211.                     lastIntervalWasAPomodoro = true;                    
  212.                     timerId = setInterval(decrementTimer, 1000);                    
  213.                     return; //DO I NEED THIS?
  214.                 }
  215.         }
  216.        
  217.         function shortBreak() {            
  218.            
  219.            
  220.             console.log("short break");                        
  221.            
  222.             document.getElementById("nextBreak").innerHTML = "On a short break";
  223.            
  224.             lastIntervalWasAPomodoro = false;      
  225.            
  226.             document.getElementById("minutes").innerHTML = shortBreakMinutes;
  227.             document.getElementById("seconds").innerHTML = shortBreakSeconds;
  228.            
  229.             timerId = setInterval(decrementTimer, 1000);
  230.            
  231.                        
  232.         }
  233.        
  234.         function longBreak() {            
  235.            
  236.            
  237.             console.log("long break");                  
  238.            
  239.             document.getElementById("nextBreak").innerHTML = "On a long break";
  240.  
  241.             lastIntervalWasAPomodoro = false;
  242.            
  243.             checkmark = 0;
  244.            
  245.             document.getElementById("minutes").innerHTML = longBreakMinutes;
  246.             document.getElementById("seconds").innerHTML = longBreakSeconds;
  247.            
  248.             timerId = setInterval(decrementTimer, 1000);            
  249.            
  250.         }
  251.        
  252.         //Make number double-digit if single-digit
  253.         function addLeftZeroIfNeeded(number) {
  254.            
  255.             console.log(number);
  256.             if((number+"").length === 1){
  257.                 number = "0"+number;                      
  258.             }
  259.            
  260.             return number;
  261.            
  262.         }
  263.        
  264.        
  265.        
  266.         var m = document.getElementById("closeModal");
  267.         m.onclick = closeModalButtonClicked;
  268.    
  269.         function closeModalButtonClicked(event) {
  270.            
  271.             if ( document.getElementById("inputPomodoro").value !== "") {
  272.                
  273.                 pomodoroMinutes = document.getElementById("inputPomodoro").value;         pomodoroMinutes = addLeftZeroIfNeeded(pomodoroMinutes);
  274.                
  275.                 document.getElementById("minutes").innerHTML = pomodoroMinutes;
  276.                 document.getElementById("seconds").innerHTML = pomodoroSeconds;
  277.            
  278.             }
  279.            
  280.             if ( document.getElementById("inputShortBreak").value !== "") {
  281.                
  282.                 shortBreakMinutes = document.getElementById("inputShortBreak").value;
  283.                 shortBreakMinutes = addLeftZeroIfNeeded(shortBreakMinutes);      
  284.                    
  285.             }  
  286.            
  287.             if ( document.getElementById("inputLongBreak").value !== "") {
  288.                
  289.                 longBreakMinutes = document.getElementById("inputLongBreak").value;
  290.                 longBreakMinutes = addLeftZeroIfNeeded(longBreakMinutes);            
  291.                
  292.                    
  293.             }  
  294.            
  295.         }
  296.        
  297.         function spawnNotification(body, title) {
  298.            
  299.           var options = {
  300.               body: body,
  301.           };
  302.        
  303.         var n = new Notification(title, options);
  304.         }
  305.  
  306.        
  307.         //function pomodoroEndNotification
  308.        
  309.        
  310.        
  311.        
  312.        
  313.         } //closing bracket from window.onload = function ()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement