How to grab the latest input value?

Codepen here: https://codepen.io/chunzg/pen/oNYqdKa

When I click clear and then ok in the alert message, I want to input a new value that is not the default 5 minutes, for example 8 minutes. However when I click start again, it always starts the timer from the previous value (eg. 5).

How do I grab the most latest input value and make sure the timer starts from this latest value and not the previous one?

It’s weird because if I type in any number on the first time, it works perfectly. It’s only after I press clear and ok, that there is this issue.

function timeChanged() {
  console.log('timeChanged called')
  return document.querySelector('#length2').value !== currentTime
}

function bStartOrPause() {
  if (timeChanged() === true) {getCustomTime2()}
	if(!intervalId2) { //if not currently counting down
		intervalId2 = setInterval(timerFunc2, 1000);
        startBBtn.textContent = 'pause'; //change btn to pause
	} else {

when you intially call the function you never verify if the time has actually changed. I got it to work with this and a global variable currentTime. The code could probably be refactored overall knowing this. You have it to set the new value after you clear the time but not on start and stop. I didn’t implement your alert window but I think it gives you the idea.

1 Like

Thanks so much, that works perfectly. I think I get it. The currentTime variable logs as undefined so I guess this is just a way to compare it to the input value, and since they will always be not equal, then the timeChanged function will always be true. And then this allows us to carry on with an if statement condition to get the getCustomTime2 function to look again at the input value which by that time is the latest one. I hope I am getting that logic right…! :slight_smile: