Soy un principiante en Javascript y estoy creando un temporizador Pomodoro y los botones son como se ven en la imagen.
Cuando el usuario hace clic en cualquiera de los 3 botones superiores, quiero que el temporizador cambie a 25:00, 5:00 o 15:00, respectivamente. Sin embargo, cuando se hace clic en él, se inicia el temporizador. Solo quiero que el temporizador se inicie una vez que se haga clic en el botón "Inicio" [Problema 1] .
En segundo lugar, cuando se llama a la función del botón de pausa, la pantalla del temporizador se detiene, sin embargo, sigue funcionando en segundo plano [ Problema 2 ] y, para ser honesto, tengo poca idea de cómo reanudar el temporizador desde donde se detuvo, a pesar de buscar en Internet. Por favor, ¿alguien puede ayudarme con esto? ¡Estoy realmente luchando aquí! ¡Gracias!
var timer = { pomodoro: 25, longBreak: 15, shortBreak: 5 } var currentTimerType = timer.pomodoro; function timer_pomodoro() { currentTimerType = timer.pomodoro; timer_start(); } function timer_short() { currentTimerType = timer.shortBreak; timer_start(); } function timer_long() { currentTimerType = timer.longBreak; timer_start(); } var minutesToAdd = 0; function timer_start() { switch(currentTimerType) { case timer.pomodoro: minutesToAdd=25; break; case timer.shortBreak: minutesToAdd=5; break; case timer.longBreak: minutesToAdd=15; break; } var currentDate = new Date(); var futureDate = new Date(currentDate.getTime() + minutesToAdd*60000); countDownDate = futureDate; var now = new Date().getTime(); document.getElementById('seconds').innerHTML = '00'; document.getElementById('minutes').innerHTML = minutesToAdd; x = setInterval(myTimer, 1000); } function myTimer() { var currentDate = new Date(); var futureDate = new Date(currentDate.getTime() + minutesToAdd*60000); var now = new Date().getTime(); var distance = countDownDate - now; var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60)); var seconds = Math.floor((distance % (1000 * 60)) / 1000); document.getElementById('seconds').innerHTML = seconds; document.getElementById('minutes').innerHTML = minutes; if (seconds == 0 & minutes == 0) { clearInterval(x); } } function pause() { clearInterval(x); }Para el Problema 1, llame a timer_start(); en todas las funciones timer_long , timer_short y timer_podomoro . Eliminar esas llamadas lo arreglará, y solo necesita llamar a timer_start() cuando se hace clic en el botón de inicio.
Para el Problema 2, en el código proporcionado, no estoy seguro de cómo fallaría, pero para continuar, querrá almacenar el valor actual del temporizador en algún lugar fuera del inicio. Por ejemplo:
var timerStart = {} function timerStart() { timerStart.pomodoro = new Date(); }