Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ejercicio Stopwatch de PUR #26

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions stopwatch-PUR/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Stopwatch</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" />
<link rel="stylesheet" href="styles.css" />
</head>
<body>
<div class="container text-center mt-5">
<h1>Stopwatch</h1>
<div id="menu" class="mt-4">
<button class="btn btn-primary" id="show-stopwatch">Cronómetro</button>
<button class="btn btn-primary" id="show-timer">Cuenta Atrás</button>
</div>

<div id="stopwatch" class="d-none">
<h2 id="stopwatch-display">00:00:00 <small>000</small></h2>
<button class="btn btn-success" id="start-stopwatch">Iniciar</button>
<button class="btn btn-warning" id="reset-stopwatch">Reiniciar</button>
<button class="btn btn-danger" id="back-to-menu-stopwatch">Volver</button>
</div>

<div id="timer" class="d-none">
<div id="timer-inputs">
<input type="text" class="time-input" id="hours" value="00" readonly />
<input type="text" class="time-input" id="minutes" value="00" readonly />
<input type="text" class="time-input" id="seconds" value="00" readonly />
</div>
<div class="btn-group mt-2">
<button class="btn btn-secondary number-btn">0</button>
<button class="btn btn-secondary number-btn">1</button>
<button class="btn btn-secondary number-btn">2</button>
<button class="btn btn-secondary number-btn">3</button>
<button class="btn btn-secondary number-btn">4</button>
<button class="btn btn-secondary number-btn">5</button>
<button class="btn btn-secondary number-btn">6</button>
<button class="btn btn-secondary number-btn">7</button>
<button class="btn btn-secondary number-btn">8</button>
<button class="btn btn-secondary number-btn">9</button>
</div>
<div class="mt-2">
<button class="btn btn-warning" id="clear-inputs">Limpiar</button>
<button class="btn btn-primary" id="set-timer">Set</button>
<button class="btn btn-danger" id="back-to-menu-timer">Volver</button>
</div>
<div id="timer-set" class="d-none">
<h2 id="timer-display">00:00:00 <small>000</small></h2>
<button class="btn btn-success" id="start-timer">Iniciar</button>
<button class="btn btn-warning" id="reset-timer">Reiniciar</button>
<button class="btn btn-danger" id="back-to-inputs">Volver</button>
</div>
</div>
</div>

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
<script src="script.js"></script>
</body>
</html>
147 changes: 147 additions & 0 deletions stopwatch-PUR/script.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
$(document).ready(function () {
let stopwatchInterval;
let timerInterval;
let isStopwatchRunning = false;
let isTimerRunning = false;
let stopwatchTime = 0;
let timerTime = 0;

// Menu buttons
$("#show-stopwatch").click(function () {
$("#menu").addClass("d-none");
$("#stopwatch").removeClass("d-none");
});

$("#show-timer").click(function () {
$("#menu").addClass("d-none");
$("#timer").removeClass("d-none");
});

$("#back-to-menu-stopwatch, #back-to-menu-timer").click(function () {
$("#stopwatch, #timer").addClass("d-none");
$("#menu").removeClass("d-none");
resetStopwatch();
resetTimer();
});

// Stopwatch functionality
$("#start-stopwatch").click(function () {
if (isStopwatchRunning) {
clearInterval(stopwatchInterval);
$("#start-stopwatch").text("Iniciar");
} else {
stopwatchInterval = setInterval(updateStopwatch, 10);
$("#start-stopwatch").text("Detener");
}
isStopwatchRunning = !isStopwatchRunning;
});

$("#reset-stopwatch").click(function () {
resetStopwatch();
});

function updateStopwatch() {
stopwatchTime += 10;
let milliseconds = parseInt((stopwatchTime % 1000) / 10);
let seconds = Math.floor((stopwatchTime / 1000) % 60);
let minutes = Math.floor((stopwatchTime / (1000 * 60)) % 60);
let hours = Math.floor((stopwatchTime / (1000 * 60 * 60)) % 24);
$("#stopwatch-display").html(
`${pad(hours)}:${pad(minutes)}:${pad(seconds)} <small>${pad(milliseconds, 3)}</small>`
);
}

function resetStopwatch() {
clearInterval(stopwatchInterval);
isStopwatchRunning = false;
stopwatchTime = 0;
$("#start-stopwatch").text("Iniciar");
$("#stopwatch-display").html("00:00:00 <small>000</small>");
}

// Timer functionality
$(".number-btn").click(function () {
let number = $(this).text();
let currentTime = getCurrentTime();
let newTime = (currentTime * 10 + parseInt(number)) % 1000000;
setTime(newTime);
});

$("#clear-inputs").click(function () {
setTime(0);
});

$("#set-timer").click(function () {
$("#timer-inputs, .btn-group, #clear-inputs, #set-timer").addClass("d-none");
$("#timer-set, #back-to-inputs").removeClass("d-none");
timerTime = getCurrentTime() * 1000;
updateTimerDisplay();
});

$("#back-to-inputs").click(function () {
$("#timer-set, #back-to-inputs").addClass("d-none");
$("#timer-inputs, .btn-group, #clear-inputs, #set-timer").removeClass("d-none");
clearInterval(timerInterval);
isTimerRunning = false;
$("#start-timer").text("Iniciar");
});

$("#start-timer").click(function () {
if (isTimerRunning) {
clearInterval(timerInterval);
$("#start-timer").text("Iniciar");
} else {
timerInterval = setInterval(updateTimer, 10);
$("#start-timer").text("Detener");
}
isTimerRunning = !isTimerRunning;
});

$("#reset-timer").click(function () {
setTime(0);
$("#back-to-inputs").click();
});

function updateTimer() {
timerTime -= 10;
if (timerTime <= 0) {
timerTime = 0;
clearInterval(timerInterval);
$("#start-timer").text("Iniciar");
isTimerRunning = false;
alert("¡Tiempo terminado!");
}
updateTimerDisplay();
}

function updateTimerDisplay() {
let milliseconds = parseInt((timerTime % 1000) / 10);
let seconds = Math.floor((timerTime / 1000) % 60);
let minutes = Math.floor((timerTime / (1000 * 60)) % 60);
let hours = Math.floor((timerTime / (1000 * 60 * 60)) % 24);
$("#timer-display").html(
`${pad(hours)}:${pad(minutes)}:${pad(seconds)} <small>${pad(milliseconds, 3)}</small>`
);
}

function getCurrentTime() {
let hours = parseInt($("#hours").val()) * 3600;
let minutes = parseInt($("#minutes").val()) * 60;
let seconds = parseInt($("#seconds").val());
return hours + minutes + seconds;
}

function setTime(time) {
let hours = Math.floor(time / 3600);
let minutes = Math.floor((time % 3600) / 60);
let seconds = time % 60;
$("#hours").val(pad(hours));
$("#minutes").val(pad(minutes));
$("#seconds").val(pad(seconds));
}

function pad(num, size = 2) {
let s = "000" + num;
return s.substr(s.length - size);
}
});
22 changes: 22 additions & 0 deletions stopwatch-PUR/styles.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
.time-input {
width: 60px;
font-size: 2rem;
text-align: center;
margin: 0 5px;
}

.btn-group .btn {
width: 40px;
height: 40px;
font-size: 1.2rem;
}

#stopwatch-display,
#timer-display {
font-size: 3rem;
}

#stopwatch-display small,
#timer-display small {
font-size: 1.5rem;
}