cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

Quick-add: + add another resource

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

Quick-add: + add another resource

Code Indentation

     

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

            
              

    <div class="container-fluid">
  <h1>Timer</h1>
</div>
<div class="container-fluid">
  <div class="row">
    <div class="col-md-5"></div>
    <div class="col-md-1">
      <div class="btn-time-header">
        BREAK LENGTH
      </div>
       <div class="btn-time-change">
        <button id="btn-bl-down" class="btn btn-default btn-timer">-</button>
         <span id="break-length" class="time-num"> 5 </span>
        <button id="btn-bl-up" class="btn btn-default btn-timer">+</button>
      </div>
    </div>
    <div class="col-md-1">
      <div class="btn-time-header">
        SESSION LENGTH
      </div>
      <div class="btn-time-change">
        <button id="btn-sl-down" class="btn btn-default btn-timer">-</button>
        <span id="session-length" class="time-num"> 25 </span>
        <button id="btn-sl-up" class="btn btn-default btn-timer">+</button>
      </div>
    </div>
    <div class="col-md-5"></div>
  </div>
  
</div>

<div class="container-fluid">
  
  <div class="timer">
    <span id="timer-fill" class="timer-fill">
    </span>
    <div class="container-fluid">
      <div id="time-type" class="timer-text">
        Session
      </div>
    </div>
    <div class="container-fluid">
      <div id="time-action" class="timer-action-text">
        Start
      </div>
      <div id="time-action-running" class="timer-action-resets">
        <span id="time-action-reset" class="fa fa-repeat">Reset</span>
        <span id="time-action-pause" style="margin-left:10px" class="fa fa-pause">Pause</span>
        <span id="time-action-resume" style="margin-left:10px" class="fa fa-play">Resume</span>
      </div>
    </div>
    <div class="container-fluid">
      <div id="time-amount" class="timer-text timer-amount-text">
        <span id="time-amt-num">
          25
        </span>
        <div id="time-amt-type">
          Minutes
        </div>
      </div>
    </div>
  </div>
  
</div>

            
          
!
            
              html, 
body{
  height: 100%;
  overflow: hidden;
  background-color: #333333;
}

/* Header styling */
/* I'm using Pacifico for headers because I like it */
h1,
h2,
h3,
h4,
h5,
h6{
  display: block;
  background-color: #333333;
  margin: 0 auto;
  color: white;
  text-align: center;
  font-family: 'Pacifico',cursive;
  font-size: 5em;
}

.timer {
  margin: 0 auto;
  text-align: center;
  width: 300px;
  height: 300px;
  font-size: 4em;
  border: 2px solid #99CC00;
  border-radius: 50%;
  cursor: pointer;
  position: relative;
  z-index: 20;
  overflow: hidden;
}

.timer-text{
  text-align: center;
  font-family: 'Open Sans', sans-serif;
  color: white;
  font-size: .6em;
  margin-top: 40px;
}

.timer-amount-text{
  margin-top: 15px;
}

.timer-action-text{
  text-align: center;
  font-family: 'Open Sans', sans-serif;
  color: white;
  font-size: .5em;
  margin-top: 20px;
}

.timer-action-resets{
  text-align: center;
  font-family: 'Open Sans', sans-serif;
  color: white;
  font-size: .4em;
  margin-top: 20px;
}

.timer:before {
  content: '';
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  border-radius: 50%;
  z-index: 2;
  border: 4px solid #333333;
}

.timer-fill{
  position: absolute;
  background: #99CC00;
  bottom: 0;
  right: 0;
  left: 0;
  z-index: -1;
  height: 0%;
}

.btn-time-header{
  font-family: 'Open Sans', sans-serif;
  color: white;
  text-align: center;
  font-size: .8em;
}

.btn-time-change{
  font-family: 'Open Sans', sans-serif;
  color: white;
  text-align: center;
  font-size: .8em;
}

.time-num{
  font-family: 'Open Sans', sans-serif;
  color: white;
  text-align: center;
  font-size: 2em;
}

.btn-timer{
  background-color: #333333;
  color: white;
}

            
          
!
            
              $(function() {
  
  $("#time-action-reset").hide();
  $("#time-action-pause").hide();
  $("#time-action-resume").hide();
  
  var timeMS = 0;
  var timeType = "session";
  var minMS = 60000;
  var timerRunning = false;
  var startMS = 0;
  var currentMS = 0;
  var audFile = 'http://www.oringz.com/oringz-uploads/sounds-917-communication-channel.mp3';
  //var audFile = 'http://www.oringz.com/oringz-uploads/sounds-948-just-like-magic.mp3';
  var paused = false;
  
  function increaseBtn(elementID){
    var elementLength = parseInt(elementID.text());
    if (elementLength === 60) {
      elementLength = 1;
    } else {
      if (elementLength < 5) {
        elementLength += 1;
      } else {
        elementLength += 5;
      }
    }
    elementID.text(elementLength.toString());
    return elementLength;
  }
  
  function decreaseBtn(elementID){
    var elementLength = parseInt(elementID.text());
    if (elementLength === 1) {
      elementLength = 60;
    } else {
      if (elementLength <= 5) {
        elementLength -= 1;
      } else {
        elementLength -= 5;
      }
    }
    elementID.text(elementLength.toString());
    return elementLength;
  }
  
  function animateTimer(timeDuration, audioFile){
      var timeMS = timeDuration;
      
     $("#timer-fill").animate({
        height: '100%'
      }, 
      {
       duration: timeMS,
       easing: "linear",
       step: function(now, fx) {

         var t = $.now();
         var timeDuration = t - startMS;
         var msLeft = timeMS - timeDuration;
         currentMS = msLeft;
         
         var secondMS = 1000;

         if (msLeft < minMS) {
           // less than a minute left
           // change the time-amt-type to seconds
           $("#time-amt-type").text("Seconds");
           // get the time left in seconds     
           var secondsLeft = Math.floor((msLeft / secondMS) % 60);
           if(!isNaN(secondsLeft) && secondsLeft >= 0)
           { 
              $("#time-amt-num").text(secondsLeft.toString()); 
           }
          
         } else {
           var minutesLeft = Math.floor((msLeft / minMS));
           secondsLeft = Math.floor((msLeft / secondMS) % 60);
           
           // format seconds to have 2 digits
           var secondsLeftVal = secondsLeft.toString();
           if(secondsLeft < 10){
             secondsLeftVal = "0" + secondsLeft.toString();
           }
           
           if(!isNaN(minutesLeft)){ 
             $("#time-amt-num").text(minutesLeft.toString() + " : " + secondsLeftVal); 
           }          
         }
         
        },
        complete: function(){
			    audioFile.play();
          
          if(timeType === "session"){
            timeType = "break";
            $("#time-type").text("Break");
            var blText = $("#break-length").text();
            $("#time-amt-num").text(blText);
            $("#time-amt-type").text("Minutes");
          }
          else{
            timeType = "session";
            $("#time-type").text("Session");
            $("#time-amt-num").text($("#session-length").text());
            $("#time-amt-type").text("Minutes");
          }
          
          $("#timer-fill").css("height","0%");
          
          if (timeType === "session") {
            timeMS = parseInt($("#session-length").text());
          } else {
            timeMS = parseInt($("#break-length").text());
          }
          
          timeMS *= minMS;
          startMS = $.now();
          animateTimer(timeMS, audioFile);
        }
     });
  }
  
  function resetTimer(){
    
      // stop the animation
      $("#timer-fill").clearQueue();
      $("#timer-fill").stop();
      // reset the clock
      $("#timer-fill").css("height","0%");
      $("#time-amt-type").text("Minutes");
      $("#time-amt-num").text($("#session-length").text());
      $("#time-type").text("Session");
      timeType = "session";
      timerRunning = false;
      
      // add buttons back
      $("#btn-bl-up").show();
      $("#btn-bl-down").show();
      $("#btn-sl-up").show();
      $("#btn-sl-down").show();
      // change text to start
      $("#time-action-reset").hide();
      $("#time-action-pause").hide();
      $("#time-action-resume").hide();
      $("#time-action").show();
      
  }
  
  function pauseTimer(){

      // stop the animation
      $("#timer-fill").clearQueue();
      $("#timer-fill").stop();
      
      var $timerFillHeight = $("#timer-fill").css('height');      
      $("#timer-fill").css("height",$timerFillHeight);
      
      $("#time-action-pause").hide();
      $("#time-action-resume").show();
      
      timerRunning = false;
      paused = true;
       
  }
  
  function startTimer(){
      startMS = $.now();
      timerRunning = true;
      // remove buttons while timer is running
      $("#btn-bl-up").hide();
      $("#btn-bl-down").hide();
      $("#btn-sl-up").hide();
      $("#btn-sl-down").hide();
      // change text to reset
      $("#time-action").hide();
      $("#time-action-reset").show();
      $("#time-action-pause").show();
      
      if (timeType === "session") { timeMS = parseInt($("#session-length").text()); } 
      else { timeMS = parseInt($("#break-length").text()); }
      
      timeMS *= minMS;
  }
  
  $("#btn-bl-up").on('click', function() {
    if(!timerRunning){ increaseBtn($("#break-length")); }
  });

  $("#btn-bl-down").on('click', function() {
    if(!timerRunning){ decreaseBtn($("#break-length")); }
  });

  $("#btn-sl-up").on('click', function() {
    
    if(!timerRunning){ 
      var sessionLength = increaseBtn($("#session-length"));
      $("#time-amt-num").text(sessionLength.toString());
    }
    
  });

  $("#btn-sl-down").on('click', function() {
    
    if(!timerRunning){ 
      var sessionLength = decreaseBtn($("#session-length"));
      $("#time-amt-num").text(sessionLength.toString());
    }
    
  });

  $(".timer").on('click', function(e) {
    var audio = new Audio(audFile);
    // pre-load the audio file to get around the 
    // weird mobile chrome block for playing audio
    audio.load();
    
    var $pauseLeft = $("#time-action-pause").offset().left;
    var $resumeLeft = $("#time-action-resume").offset().left;
    var $clickX = e.pageX;
    
    console.log("Pause left: " + $pauseLeft + " resume left: " + $resumeLeft + " click X: " + $clickX);
    
    if(paused){
      // if paused before click options are resume
      // and reset
      paused = false;
      if($clickX < $resumeLeft){ resetTimer();}
      else{
        // resume           
        startMS = $.now();
        timeMS = currentMS;
        $("#time-action-resume").hide();
        $("#time-action-pause").show();
        timerRunning = true; 
      }
    }
    else if (timerRunning){
        // if the timer is running and it's clicked
        // options are pause and reset
        if($clickX < $pauseLeft){ resetTimer(); }
        else{ pauseTimer(); }
    } 
    else {
      // not paused and timer not running before click
      // start the timer
      startTimer();
    }

    if(timerRunning){ animateTimer(timeMS, audio);  }

  });

});
            
          
!
999px
Loading ..................

Console