When I make a copy of my constructor with new, it automatically starts a method, how do I stop this

When I make a copy of my constructor with new, it automatically starts a method, how do I stop this
0.0 0

#1

I have a constructor called timer with function methods inside of it. the start method inside of the timer constructor is an immediately invoked function and it is activating when I make my new object (sessionCountdown) to assign timer to, but I don’t want it to activate immediately when I assign timer to sessionCountdown, I want it to activate when I call sessionCountdown.start(3), something like that. I tried wrapping the start method inside a function earlier, but then when I called it like timer.start(2) it wasn’t working so I left it as an immediately invoked function.

The new object I made to assign timer to is sessionCountdown, but I don’t want the start method to activate when I assign sessionCountdown to the timer constructor, I will appreciate any help with how to do this.

here are links to my code : https://pastebin.com/PVuv0fky https://codepen.io/icewizard/pen/KQaGJe?editors=0000


#2

click the three dots on the bottom right of your post and you will see a trash can click on it and it will be deleted


#3

What do you mean your question was bad? Did you figure out the answer to your question? If so, leave your original content and reply back with how you resolved your issue.

Do you still a different question you want to ask? If so, then edit your post with the new question.


#4

I have a new question. How do I stop a method from being declared if I assign the constructor its in to an object.

I am doing this let sessionCountdown = new timer(sessionNumber2); , but the method inside timer called start activates when I assign timer to sessionCountdown. I don’t want this to happen, I want start to activate when I use sessionCountdown.start(4). inside start is an immediately invoked function, earlier I tried wrapping that function inside of another function to stop it from being invoked but then the start method wouldn’t work when I invoked it.


#5

Does you Codepen currently reflect the code you describe in your latest post? If so, I will take a few minutes to review your code and try to answer your question.


#6

Yes, but all the important functions are in the pastebin I linked. I’m also editing it to try to make some stuff work right now.


#7

Put everything you want me to look at in one or both and let me know. If you want to continue working, then fork it, so the link on the forum points to what you want help with.


#8

here is the forked version with everything in my problem https://codepen.io/icewizard/pen/RQdRXe?editors=0010 the timer constructor and the sessionCountdown object are at the very bottom of the javascript side.


#9

The following line within timer is what starts the interval.

this.start = setInterval(function() {

this.start is not a function. It holds the interval id which can be used later to clear the interval. When you write the setInterval, that code in the callback function will execute every 1 1 second.

What does the 4 represent above? I am not understanding where it would come from and what it’s purpose is.


#10

in timer I made the parameter called sessionNumber2 so the timer knows what number to countdown from. It’s counting down from 1 minute right now because sessionNumber2 is 1.

let timer = function(sessionNumber2)

so I would call it like timer(someNumber), I just used 4 to give an example of a parameter, it would show 4 minutes.

or should I call it like sessionCountdown(4).start()? I’m not really sure but I tried this earlier when I tried wrapping a funciton around the start methods immediately invoked setInterval function, and nothing happened.

and my main issue is when I made sessionCountdown here and assigned it to timer

let sessionCountdown = new timer(sessionNumber2);

this.start or the function setInterval inside of it immediately started, but I only want it to start when I call it, I’m not sure how to do this.


#11

I just solved it I had to use this.intervalRef = and wrap it in a function. thank you for your help

I can call it like sessionCountdown.start() now

this.start = function(){
      this.intervalRef = setInterval(function(){
        
        startTimer = true; 
        //get Todays date and time
        const now = new Date().getTime();

        //Find the distance between now and count down date
        const 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;
            }
        }
      
        //paused
        
        if (session === false){
          clearInterval(this.start);
          
        }

        time.innerHTML = minutes + ':' + seconds;

        //if over do something
        if (distance <= 0){
            clearInterval(start);
            timeName.innerHTML = 'break';
            time.innerHTML = 'done';
        }


    }, 1000)}

EDIT: I found this solution on another programming help forum that I stumbled upon and it works, but I’m not sure how. Earlier when I had this problem and couldn’t solve it I tried wrapping it in a function and invoking it the same way here, but I did not have this.intervalRef assigned to my setInterval function. I don’t know why intervalRef is causing this function to work though and I’d appreciate if anyone could explain it to me.

or link me to something like it that explains what it does. I’ve been googling and can find nothing, probably has something to do with this rules though, I’ll need to read you dont know javascript again…


#12

If you want a start method for your timer function, then you could write:

let currentInterval 
/* this will hold the interval id so you can clear it when needed.  
 Note, it is outside the start function but is still local to the timer function. */

this.start = function() {
  // the following needs to be here, so the start button shows until start is invoked.   
  startButton.style.display = "none";
  pause.style.display = "unset";
  const then = new Date();
  const end = then.setMinutes(then.getMinutes() + sessionNumber2);  
  // the above needs to be here, so the start button shows until start is invoked.  

  // now you execute the setInterval and assign it's interval to currentInterval
  setInterval(function() {
   .
   .  
   // original code goes here
   . 
   .   
  }, 1000);
};

EDIT: Looks like you already found a solution which is similar to what I have suggested above.


#13

This is better than what I found, thanks a lot.


#14

Not sure if you are aware, but your timer currently starts displaying at 0:58 instead of 0:59 after the first second (first iteration of the setInterval). There is a small issue with your logic inside the callback function which is causing this.


#15

I’ve noticed that but I’m going to fix that later, right now I want to focus on fixing bigger issues and adding functions. Thanks for reminding me.