Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // create window.TIMER
- window.TIMER = window.TIMER || {};
- // begin init function
- var init = function() {
- var activeTime = 0; // number of seconds active timer should run for
- var active_timer_is_on = false; // flag for active timer
- var intervals_left=0; //intializing interval counter
- var number_of_intervals = 0; // number of intervals the workout timer should perform.
- var nextPhase = ""; // number of intervals remaining while the timer is running.
- var restTime = 0; // number of seconds rest timer should run fo
- var rest_timer_is_on = false; // flag for rest time
- TIMER.time_left = 0; //intialize timer counter
- var timer_is_on = false; // flag for workout timer
- var wt; // initializing timeout variable
- // function to switch phases mid interval
- var switchPhase = function(){
- if (active_timer_is_on === true){
- active_timer_is_on = false;
- rest_timer_is_on = true;
- return;
- }
- if (rest_timer_is_on === true){
- rest_timer_is_on = false;
- active_timer_is_on = true;
- } else {
- console.log('both timers in "switchPhase" are false')
- }
- };
- // function to output how much time and intervals are left in timer
- // console.log is for debugging purposes and will be removed from final version
- var msgGenerator = function(time, phase, intervals){
- if (intervals > 1){
- console.log(time + " seconds left until the next " + phase + " phase and you have " + intervals + " intervals left.");
- return time + "<br/>" + intervals + " intervals left.";
- }
- if (intervals === 1){
- console.log (time + " seconds left until the next " + phase + " phase and you have " + intervals + " interval left.");
- return time + "<br/>" + intervals + " interval left.";
- }
- if (intervals === 0 && phase === "rest"){
- console.log (time + " seconds left in this active phase and this is the last interval.");
- return time + "<br /> Last active phase. Long rest after this. You can do it!";
- }
- if (intervals === 0 && phase === "active"){
- console.log (time + " seconds left until you are done!");
- return time + "<br /> The last rest phase! You are almost done!";
- }
- if (intervals < 0){
- console.log ("YOUR ARE DONE!");
- return "FINISHED";
- }
- console.log ("no msg generated " + intervals + " intervals left");
- return;
- };
- // function that actually counts down the time
- TIMER.timedCount = function(time_left) {
- if (time_left >= 0){
- document.getElementById('counter').innerHTML = msgGenerator(time_left, nextPhase, intervals_left);
- time_left--;
- wt=setTimeout("TIMER.timedCount("+time_left+")",1000);
- } else {
- clearTimeout(wt);
- switchPhase();
- fartlekCounter();
- }
- }
- // checks interval count
- var fartlekCounter = function() {
- if (intervals_left > 0 && active_timer_is_on === true){
- //time_left = activeTime;
- nextPhase = "rest";
- intervals_left--;
- document.getElementById('counter').style.background = "green";
- TIMER.timedCount(activeTime);
- } else if (intervals_left >= 0 && rest_timer_is_on === true){
- //time_left = restTime;
- nextPhase = "active";
- document.getElementById('counter').style.background = "red";
- TIMER.timedCount(restTime);
- } else{
- timer_is_on = false;
- document.getElementById('counter').innerHTML = msgGenerator(intervals_left);
- console.log("YOU ARE DONE!");
- }
- };
- //starts the timer
- TIMER.doTimer = function () {
- if (timer_is_on === true){
- return;
- }
- activeTime = document.getElementById('active_time').value;
- number_of_intervals = document.getElementById('number_of_intervals').value;
- restTime = document.getElementById('rest_time').value;
- timer_is_on = true;
- active_timer_is_on = true;
- intervals_left = number_of_intervals;
- fartlekCounter();
- }
- //Stop Timer
- TIMER.stopCount = function (){
- clearTimeout(wt);
- timer_is_on = false;
- }
- // end init function
- };
- $(document).ready(init);
- //var add_event_handlers = function() {
- // $('#start_btn').on('click', TIMER.doTimer);
- //};
Add Comment
Please, Sign In to add comment