Created
November 28, 2011 23:47
-
-
Save scichelli/1402649 to your computer and use it in GitHub Desktop.
JS RunningTimer, before refactoring
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$('.runDisplay').hide(); | |
var RT = { | |
setup : {}, | |
warmupDuration : 5, | |
cooldownDuration : 5 | |
}; | |
var runProgram = function() { | |
RT.setup = readSetup(); | |
$('.runSetup').hide(); | |
displayHeading(RT.setup.week, RT.setup.day); | |
$('.runDisplay').show(); | |
warmup(RT.setup); | |
}; | |
var restartProgram = function() { | |
location.reload(); | |
} | |
var readSetup = function() { | |
return { | |
week : $('#ddlWeek').val(), | |
day : $('input:radio[name=radDay]:checked').val(), | |
includeWarmup : $('#chkWarmup:checked').val() !== undefined | |
}; | |
}; | |
var displayHeading = function(week, day) { | |
$('.runDisplay h1').text('Week ' + week + ', Day ' + day); | |
}; | |
var warmup = function(setup) { | |
if (setup.includeWarmup) { | |
transition(function() { walk('Warm Up'); }); | |
window.setTimeout(function() {transition(function() { exercise(setup); })}, minToMilli(RT.warmupDuration)); | |
} else { | |
transition(function() { exercise(setup); }); | |
} | |
} | |
var exercise = function(setup) { | |
var workout = getWorkout(setup.week, setup.day); | |
doExercise(workout, 0); | |
} | |
var cooldown = function() { | |
walk('Cool Down'); | |
window.setTimeout(function() { transition(function() { walk('Done!'); })}, minToMilli(RT.cooldownDuration)); | |
} | |
var doExercise = function(workout, i) { | |
workout[i].mode(); | |
if (i === workout.length - 1) { | |
window.setTimeout(function() { transition(cooldown) }, minToMilli(workout[i].minutes)); | |
} else { | |
window.setTimeout(function() { transition(function() { doExercise(workout, i + 1); })}, minToMilli(workout[i].minutes)); | |
} | |
} | |
var getWorkout = function(week, day) { | |
return eval('C25K.W' + week + 'D' + day); | |
} | |
var transition = function(callback) { | |
var indicatorDiv = $('.runCountdown'); | |
var body = $(document.body); | |
transitionBlink(6, indicatorDiv, body, callback); | |
} | |
var transitionBlink = function(transitionStage, indicatorDiv, body, callback) { | |
if (transitionStage > 0) { | |
if (transitionStage % 2 === 0) { | |
body.attr('class', 'transitionA'); | |
indicatorDiv.text(transitionStage / 2 + '...'); | |
} else { | |
body.attr('class', 'transitionB'); | |
} | |
window.setTimeout(function() { transitionBlink(transitionStage - 1, indicatorDiv, body, callback) }, 500); | |
} else { | |
indicatorDiv.text(''); | |
window.setTimeout(callback(), 500); | |
} | |
} | |
var minToMilli = function(minutes) { | |
return minutes * 60 * 1000; | |
} | |
var walk = function() { | |
var walkText = arguments[0] || 'Walk'; | |
$(document.body).attr('class', 'walk'); | |
$('.runIndicator').text(walkText); | |
} | |
var run = function() { | |
$(document.body).attr('class', 'run'); | |
$('.runIndicator').text('Run'); | |
} | |
$('#btnStart').click(runProgram); | |
$('#btnRestart').click(restartProgram); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var week1 = [ | |
{ mode: run, minutes: 1 }, | |
{ mode: walk, minutes: 1.5 }, | |
{ mode: run, minutes: 1 }, | |
{ mode: walk, minutes: 1.5 }, | |
{ mode: run, minutes: 1 }, | |
{ mode: walk, minutes: 1.5 }, | |
{ mode: run, minutes: 1 }, | |
{ mode: walk, minutes: 1.5 }, | |
{ mode: run, minutes: 1 }, | |
{ mode: walk, minutes: 1.5 }, | |
{ mode: run, minutes: 1 }, | |
{ mode: walk, minutes: 1.5 }, | |
{ mode: run, minutes: 1 }, | |
{ mode: walk, minutes: 1.5 }, | |
{ mode: run, minutes: 1 }, | |
{ mode: walk, minutes: 1.5 }, | |
{ mode: run, minutes: 1 } | |
]; | |
var week2 = [ | |
{ mode: run, minutes: 1.5 }, | |
{ mode: walk, minutes: 2 }, | |
{ mode: run, minutes: 1.5 }, | |
{ mode: walk, minutes: 2 }, | |
{ mode: run, minutes: 1.5 }, | |
{ mode: walk, minutes: 2 }, | |
{ mode: run, minutes: 1.5 }, | |
{ mode: walk, minutes: 2 }, | |
{ mode: run, minutes: 1.5 }, | |
{ mode: walk, minutes: 2 }, | |
{ mode: run, minutes: 1.5 } | |
]; | |
var week3 = [ | |
{ mode: run, minutes: 1.5 }, | |
{ mode: walk, minutes: 1.5 }, | |
{ mode: run, minutes: 3 }, | |
{ mode: walk, minutes: 3 }, | |
{ mode: run, minutes: 1.5 }, | |
{ mode: walk, minutes: 1.5 }, | |
{ mode: run, minutes: 3 }, | |
{ mode: walk, minutes: 3 }, | |
{ mode: run, minutes: 1.5 } | |
]; | |
var week4 = [ | |
{ mode: run, minutes: 3 }, | |
{ mode: walk, minutes: 1.5 }, | |
{ mode: run, minutes: 5 }, | |
{ mode: walk, minutes: 2.5 }, | |
{ mode: run, minutes: 3 }, | |
{ mode: walk, minutes: 1.5 }, | |
{ mode: run, minutes: 5 } | |
]; | |
var C25K = { | |
W1D1 : week1, | |
W1D2 : week1, | |
W1D3 : week1, | |
W2D1: week2, | |
W2D2: week2, | |
W2D3: week2, | |
W3D1: week3, | |
W3D2: week3, | |
W3D3: week3, | |
W4D1: week4, | |
W4D2: week4, | |
W4D3: week4, | |
W5D1 : [ | |
{ mode: run, minutes: 5 }, | |
{ mode: walk, minutes: 3 }, | |
{ mode: run, minutes: 5 }, | |
{ mode: walk, minutes: 3 }, | |
{ mode: run, minutes: 5 } | |
], | |
W5D2 : [ | |
{ mode: run, minutes: 8 }, | |
{ mode: walk, minutes: 5 }, | |
{ mode: run, minutes: 8 } | |
], | |
W5D3 : [ | |
{ mode: run, minutes: 20 } | |
], | |
W6D1 : [ | |
{ mode: run, minutes: 5 }, | |
{ mode: walk, minutes: 3 }, | |
{ mode: run, minutes: 8 }, | |
{ mode: walk, minutes: 3 }, | |
{ mode: run, minutes: 5 } | |
], | |
W6D2 : [ | |
{ mode: run, minutes: 10 }, | |
{ mode: walk, minutes: 3 }, | |
{ mode: run, minutes: 10 } | |
], | |
W6D3 : [ | |
{ mode: run, minutes: 25 } | |
], | |
W7D1 : [ | |
{ mode: run, minutes: 25 } | |
], | |
W7D2 : [ | |
{ mode: run, minutes: 25 } | |
], | |
W7D3 : [ | |
{ mode: run, minutes: 25 } | |
], | |
W8D1 : [ | |
{ mode: run, minutes: 28 } | |
], | |
W8D2 : [ | |
{ mode: run, minutes: 28 } | |
], | |
W8D3 : [ | |
{ mode: run, minutes: 28 } | |
], | |
W9D1 : [ | |
{ mode: run, minutes: 30 } | |
], | |
W9D2 : [ | |
{ mode: run, minutes: 30 } | |
], | |
W9D3 : [ | |
{ mode: run, minutes: 30 } | |
] | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment