gears/timer.js

244 lines
5.3 KiB
JavaScript
Raw Normal View History

2019-11-10 19:54:50 +00:00
window.timer = {};
timer.maxTime;
timer.timeLeft;
timer.paused = false;
timer.stopped = true;
timer.over = false;
timer.chime = false;
window.stopwatch = {};
stopwatch.begin;
stopwatch.time;
stopwatch.stopped = true;
stopwatch.paused = false;
stopwatch.laps = 0;
window.options = {
timerType: "timerMode"
}
resetClock = function()
{
stopwatch.stop();
timer.stop();
timer.over = true;
$("#startStop").unbind("click");
$("#timerPause").unbind("click");
if (options.timerType == "timerMode")
{
$("#lapContainer").css("display", "none");
$("#startStop").on("click", timer.toggleStartStop);
$("#timerPause").on("click", timer.pause);
$("#timerSetup").css("display", "block");
}
if (options.timerType == "stopwatchMode")
{
$("#lapContainer").css("display", "block");
$("#startStop").on("click", stopwatch.toggleStartStop);
$("#timerPause").on("click", stopwatch.pause);
$("#timerSetup").css("display", "none");
}
$("#timerPause").attr("disabled", true);
$("h3 time").text("00:00:00");
}
stopwatch.addLap = function()
{
$("#lapContainer label").text(++stopwatch.laps);
}
stopwatch.start = function()
{
stopwatch.time = moment.duration(0);
$("#startStop").val("Reset");
$("#timerPause").attr("disabled", false);
$("#timerPause").val("Pause");
stopwatch.stopped = false;
stopwatch.paused = false;
stopwatch.laps = 0;
$("#lapContainer label").text("0");
}
stopwatch.stop = function()
{
stopwatch.time = moment.duration(0);
$("#startStop").val("Start");
$("#timerPause").attr("disabled", true);
$("#timerPause").val("Pause");
stopwatch.paused = false;
stopwatch.stopped = true;
stopwatch.laps = 0;
$("#lapContainer label").text("0");
}
stopwatch.toggleStartStop = function()
{
if (stopwatch.stopped)
stopwatch.start();
else
stopwatch.stop();
}
stopwatch.pause = function()
{
if (stopwatch.paused)
{
$("#timerPause").val("Pause");
stopwatch.paused = false;
}
else
{
$("#timerPause").val("Unpause");
stopwatch.paused = true;
}
}
stopwatch.drawNumbers = function()
{
$("h3 time").text(timer.format(stopwatch.time));
}
stopwatch.increment = function()
{
if (!stopwatch.stopped && !stopwatch.paused)
stopwatch.time.add(50, "milliseconds");
if (stopwatch.time != undefined)
stopwatch.drawNumbers();
}
timer.start = function()
{
timer.begin = moment();
timer.paused = false;
timer.maxTime = moment().add($("#timerHours").val(), "hours").add($("#timerMinutes").val(), "minutes").add($("#timerSeconds").val(), "seconds").add(1, "seconds");
timer.stopped = false;
timer.over = false;
timer.timeLeft = moment.duration(timer.maxTime.diff(timer.begin));
$("#startStop").val("Reset");
$("#timerPause").attr("disabled", false);
$("#timerPause").val("Pause");
document.getElementById("chime").fastSeek(0);
}
timer.stop = function()
{
timer.paused = false;
timer.stopped = true;
if (!timer.over)
{
timer.begin = moment();
timer.maxTime = moment().add($("#timerHours").val(), "hours").add($("#timerMinutes").val(), "minutes").add($("#timerSeconds").val(), "seconds");
timer.timeLeft = moment.duration(timer.maxTime.diff(timer.begin));
}
$("#startStop").val("Start");
$("#timerPause").attr("disabled", true);
document.getElementById("chime").pause();
document.getElementById("chime").fastSeek(0);
}
timer.pause = function()
{
if (timer.paused)
{
$("#timerPause").val("Pause");
timer.paused = false;
}
else
{
$("#timerPause").val("Unpause");
timer.paused = true;
}
}
timer.finish = function()
{
timer.over = true;
$("#timerPause").attr("disabled", true);
if ($("#chimeBool").is(":checked"))
document.getElementById("chime").play();
}
timer.wait = function()
{
if (!timer.over)
timer.finish();
}
timer.toggleStartStop = function()
{
if (timer.stopped)
{
timer.start();
console.log("timer started")
}
else timer.stop();
}
timer.drawNumbers = function()
{
$("h3 time").text(timer.format(timer.timeLeft));
if (timer.timeLeft.get("seconds") <= 0 && timer.timeLeft.get("minutes") <= 0 && timer.timeLeft.get("hours") <= 0)
{
$("h3 time").text("00:00:00");
timer.wait();
}
}
timer.format = function(prop)
{
var ret = "";
ret += timer.pad(prop.get("hours"));
ret += ":"
ret += timer.pad(prop.get("minutes"));
ret += ":"
ret += timer.pad(prop.get("seconds"));
return ret;
}
timer.pad = function(n)
{
if (n <= 9)
return "0" + n;
else return n;
}
timer.increment = function()
{
switch(options.timerType)
{
case "timerMode":
if (!timer.stopped && !timer.paused && timer.format(timer.timeLeft) != undefined)
timer.timeLeft.subtract(50, "milliseconds");
if (timer.timeLeft != undefined)
timer.drawNumbers();
break;
case "stopwatchMode":
if (!stopwatch.stopped && !stopwatch.paused)
stopwatch.time.add(50, "milliseconds");
if (stopwatch.time != undefined)
stopwatch.drawNumbers();
break;
}
}
window.updateFuncs = [ resetClock ];
$(document).ready(function(){
initOption("#timerType");
$("#addLap").on("click", stopwatch.addLap);
$("#lapContainer").css("display", "none");
$("#startStop").on("click", timer.toggleStartStop);
$("#timerPause").on("click", timer.pause)
$("#timerPause").attr("disabled", true);
window.clock = setInterval(timer.increment, 50);
})