Tengo un temporizador y un tiempo. El formato de tiempo es como la variable currentTime en mi código. Quiero obtener la hora actual y agregarle +1 cada segundo manteniendo el mismo formato de hora. También me gustaría restar el tiempo actual + 1: el total de horas pero manteniéndolo en tiempo real con setInterval. Espero haber sido claro en mis preguntas. Gracias.
HTML
<button id="start">START</button> <button id="pause">PAUSE</button> <div id="output"></div>Jav script
const startTimeButton = document.querySelector("#start") const pauseTimeButton = document.querySelector("#pause") const output = document.querySelector("#output"); let currentTime = "12: 42: 17"; let totalHours = 30; let seconds = 0; let interval = null; const timer = () => { seconds++; // Get hours let hours = Math.floor(seconds / 3600); // Get minutes let minutes = Math.floor((seconds - hours * 3600) / 60); // Get seconds let secs = Math.floor(seconds % 60); if (hours < 10) { hours = `0${hours}`; } if (minutes < 10) { minutes = `0${minutes}`; } if (secs < 10) { secs = `0${secs}`; } return `${hours}:${minutes}:${secs}`; }; startTimeButton.addEventListener("click", () => { pauseTimeButton.style.display = "flex"; startTimeButton.style.display = "none"; console.log("START TIME CLICKED"); if (interval) { return; } interval = setInterval(timer, 1000); }); pauseTimeButton.addEventListener("click", () => { pauseTimeButton.style.display = "none"; startTimeButton.style.display = "flex"; console.log("PAUSE TIME CLICKED"); clearInterval(interval); interval = null; }); // Here is an example of what I would like to achive // currentTime + 1 (each second) // output.innerHTML = (parseInt(currentTime) + 1) - totalHours;