Last active
December 28, 2015 07:19
-
-
Save avanishgiri/7464077 to your computer and use it in GitHub Desktop.
Problem 4 solution. Uses jQuery for DOM manipulation.
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
<!DOCTYPE HTML> | |
<html> | |
<head> | |
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script> | |
<script type="text/javascript" src="timer.js"></script> | |
</head> | |
<input id="seconds" type="text" value="0"> | |
<div id="countdown"> | |
<span class="days">0 days</span> | |
<span class="hours">0 hours</span> | |
<span class="minutes">0 minutes</span> | |
<span class="seconds">0 seconds</span> | |
<button class="start">START</button> | |
<button class="stop">STOP</button> | |
<button class="reset">RESET</button> | |
</div> | |
<div id="countup"> | |
<span class="days">0 days</span> | |
<span class="hours">0 hour</span> | |
<span class="minutes">0 minutes</span> | |
<span class="seconds">0 seconds</span> | |
<button class="start">START</button> | |
<button class="stop">STOP</button> | |
<button class="reset">RESET</button> | |
</div> | |
</html> |
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 UNITS = ["days","hours","minutes","seconds"]; | |
var calculate_times = function(seconds){ | |
var days = Math.floor(seconds / 86400); | |
seconds %= 86400; | |
var hours = Math.floor(seconds / 3600);; | |
seconds %= 3600; | |
var minutes = Math.floor(seconds / 60); | |
seconds %= 60; | |
return [days, hours, minutes, seconds]; | |
} | |
var is_int = function(value){ | |
return (parseFloat(value) == parseInt(value)) && !isNaN(value) | |
} | |
var render_time = function($spans, time_values){ | |
var display; | |
$spans.each(function(i){ | |
if(time_values[i] == 1) | |
display = time_values[i] + ' ' + UNITS[i].slice(0,-1); | |
else | |
display = time_values[i] + ' ' + UNITS[i]; | |
$(this).text(display); | |
}); | |
} | |
var toggle_buttons = function(selector,state){ | |
$(selector + '>button:not(:last-child').prop('disabled',!state); | |
} | |
$(function(){ | |
var seconds_left = seconds_elapsed = max_seconds = 0; | |
var down_interval, up_interval; | |
var down_interval_off = up_interval_off = false; | |
var $countdown_spans = $('#countdown > span'); | |
var $countup_spans = $('#countup > span'); | |
var countdown = function(){ | |
render_time($countdown_spans, calculate_times(--seconds_left)); | |
if(seconds_left === 0){ | |
toggle_buttons('#countdown',false); | |
return clearInterval(down_interval); | |
} | |
} | |
var countup = function(){ | |
render_time($countup_spans, calculate_times(++seconds_elapsed)); | |
if(seconds_elapsed >= max_seconds){ | |
toggle_buttons('#countup',false); | |
return clearInterval(up_interval); | |
} | |
} | |
$('#seconds').on("keyup",function(e){ | |
clearInterval(down_interval); clearInterval(up_interval); | |
up_interval_off = down_interval_off = true; | |
var input = $(this).val(); | |
if(!is_int(input) || input < 0) | |
input = 0; | |
down_interval_off = up_interval_off = true; | |
max_seconds = seconds_left = input; | |
seconds_elapsed = 0; | |
render_time($countdown_spans, calculate_times(seconds_left)); | |
render_time($countup_spans, calculate_times(seconds_elapsed)); | |
toggle_buttons('#countup',true); | |
toggle_buttons('#countdown',true); | |
}); | |
$('#countdown > .start').on("click",function(){ | |
if(down_interval_off && max_seconds > 0){ | |
down_interval = setInterval(countdown,1000); | |
down_interval_off = false; | |
} | |
}); | |
$('#countup > .start').on("click",function(){ | |
if(up_interval_off && max_seconds > 0){ | |
up_interval = setInterval(countup,1000); | |
up_interval_off = false; | |
} | |
}); | |
$('.stop').on("click",function(){ | |
if($(this).parent().attr('id') == "countdown"){ | |
clearInterval(down_interval); | |
down_interval_off = true; | |
} | |
else { | |
clearInterval(up_interval); | |
up_interval_off = true; | |
} | |
}); | |
$('#countdown > .reset').on("click",function(){ | |
seconds_left = max_seconds; | |
render_time($countdown_spans, calculate_times(seconds_left)); | |
clearInterval(down_interval); | |
down_interval_off = true; | |
toggle_buttons('#countdown',true); | |
}); | |
$('#countup > .reset').on("click",function(){ | |
seconds_elapsed = 0; | |
render_time($countup_spans, calculate_times(seconds_elapsed)); | |
clearInterval(up_interval); | |
up_interval_off = true; | |
toggle_buttons('#countup',true); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment