If statement for resume function keeps resetting time or not moving time

If statement for resume function keeps resetting time or not moving time
0.0 0

#1

I have a boolean called resuming in my Timer constructor with functions for a countdown. Inside the start function begins the timer countdown, the pause function clears the setInterval in the start function to pause it, and I’ve been trying to get the resume function to resume the time, but everything I’ve tried is resetting the time when the resume function is called, or stopping the time when it is called.

the boolean resuming starts out false, but when I activate the resuming function it turns true, and inside my start function with the countdown logic there is an if statement that does stuff if resuming is true. to calculate the distance I’m counting down from in my start function, I have the variable called now, which takes the current time with new Date, the variable end which takes the time I’m counting down too, and distance which subtracts those times to show the counting down.

I have a parameter in my start function that I use when resuming is true, it passes the current time into the start function with a variable called play using new Date and I’ve been trying to use that to resume the countdown but instead the countdown just keeps resetting from the beginning, I’m not sure why.
I’ll appreciate any help with this.

here is a link to my codepen: https://codepen.io/icewizard/pen/KQaGJe?editors=0000

       //testing resume
            if (resuming === true){
           now = trueResume;
      }


   this.resumed = function (){
        resume.style.display='none';
        pause.style.display='unset';
        resuming = true;
          const play = new Date().getTime();
        console.log(Boolean(resuming) + ' resumed' + ' ' + play);
      this.start(play);
    } 

#2

Before you figure out the pause/resume functionality, you need to resolve a couple other issues you have. When I set the session and break to 1 minute each, the session timer starts at 3 minutes and instead of counting down each second, every so often it jumps 2 seconds randomly. Sometimes it pauses for 2 seconds and then continues on. This is all without clicking anything after the original start.

Test your code and validate these issues for yourself. I have a feeling the issues I am seeing are affecting other parts of your solution, so you need to resolve this issue first.


#3

I couldn’t figure it out the way I was trying to do it when I made this thread, so I found another solution using an if statement with true or false. If a variable called session is true, run the setInterval, if it is false then pause it. It’s working now but I see some flaws in how I made the timer now. I made it based off of new Date and adding minutes to the current time and then counting down to it, but time in real life doesn’t pause, so when I resume my timer the time jumps the appropriate time that it had stayed paused for so it correctly shows how much time it will take to get to the time I’m counting down to. Instead I shouldn’t be counting down using the date, but I should make variables representing minutes and seconds and countdown with those disregarding the time in real life.

Anyway here is the working code, I’m going to remove the new date though -


 let timer = function(sessionNumber2){
   
   let session = true;
   console.log(Boolean(session));
   let currentInterval; 
    //update counter every 1 second
 
    this.start = function(){
      //hide start buttun and show other pause
      startButton.style.display = 'none';
      
      pause.style.display = 'unset';
     
      const then = new Date();
      
      const end = then.setMinutes(then.getMinutes() + sessionNumber2);
   
      function countDown(){
         if(session == true){
        //get Todays date and time
        let now = new Date().getTime();

        //Find the distance between now and count down date
        distance = end - now;
        
          
           
        // Time calculations for minutes and seconds
        let minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
        let seconds = Math.floor((distance % (1000 * 60)) / 1000);

        if (minutes < 10){
            minutes = '0' + minutes;
            if (seconds < 10){
                seconds = '0' + seconds;
            }
        }

        time.innerHTML = minutes + ':' + seconds;
        console.log(distance);
        //if over do something
        if (distance <= 0){
            clearInterval(start);
            timeName.innerHTML = 'break';
            time.innerHTML = 'done';
        }
         }
       }
    
      currentInterval = setInterval(countDown, 1000)
      
    }
  
    //paused

    this.paused = function (){
        pause.style.display='none';
        resume.style.display='unset';
        session = false;
      console.log(Boolean(session));
      }
    
    //resume

    this.resumed = function (){
        resume.style.display='none';
        pause.style.display='unset';
        session = true;
      console.log(Boolean(session));
    } 
} 

#4

thanks. I don’t think they’re really affecting the parts I’m adding right now too much since I have pause and resume working now, but I’m going to fix them now. Right now I’m hard coding in the time to countdown though because setting the time is broken, a reference error to them is occurring probably.


#5

I mean resume for job !)) That is funny setuation! I 've been writing Cv for all, and now I want to share my resource https://craftresumes.com/cvwriting/ there a team can do everything that you need!)