Added comments, incDec function

This commit is contained in:
2024-05-17 21:17:52 +02:00
parent 9f845ea234
commit 35a0785bf3
3 changed files with 39 additions and 5 deletions
+18 -1
View File
@@ -13,8 +13,10 @@ function pause() {};
function reset() {};
function end() {};
function print() {};
function incDec() {};
function getValues() {};
// Start timer not paused and not already finished
function start() {
if(isPaused == true && durationLeft.asSeconds() != 0) { // Only allow start if timer ist currently paused and durationLeft is not 0 seconds
console.log("Timer gestartet")
@@ -32,6 +34,7 @@ function start() {
return false
}
// If possible pause timer, if not return false
function pause() {
if(!isPaused) {
console.log("Timer pausiert");
@@ -42,6 +45,7 @@ function pause() {
return false
}
// Reset timer to passed value, and send durationLeft to all clients
function reset(newDuration) {
console.log("Timer Zurückgesetzt");
duration = moment.duration(newDuration, 'seconds').clone(); // Set initial duration to received duration in seconds
@@ -49,18 +53,31 @@ function reset(newDuration) {
io.sockets.emit('timerDurationLeft', print()) // Emit durationLeft to all connected sockets
}
// Stop timer and emit timerEndet to all clients
function end() {
isPaused = true // Set status of the timer
clearInterval(timerInterval); // End the execution
io.sockets.emit('timerEnded', print()) // Emit end to all connected sockets
}
// Return formatted timestamp in the format of 00:00
function print() {
var minutes = (durationLeft.minutes() < 10) ? "0" + durationLeft.minutes() : durationLeft.minutes(); // Create leading zeros for numbers <10
var seconds = (durationLeft.seconds() < 10) ? "0" + durationLeft.seconds() : durationLeft.seconds(); // Create leading zeros for numbers <10
return minutes + ":" + seconds
}
// Increase or decrease the timer depending on the passed value in seconds
function incDec(value) {
if(Math.abs(value) >= durationLeft.asSeconds()) { // If abs from passed value is greater than seconds left on timer, end timer
end();
} else {
durationLeft.add(value, 'second');
io.sockets.emit('timerDurationLeft', print())
}
}
// Return all important timer values
function getValues() {
return {
isPaused: isPaused,
@@ -71,5 +88,5 @@ function getValues() {
}
module.exports = {
duration, durationLeft, isPaused, start, pause, reset, end, print, getValues
duration, durationLeft, isPaused, start, pause, reset, end, print, incDec, getValues
}