Hi everyone,
I am working on my Pomodoro timer and I’m finding myself chasing my own tail and need some help.
But first some background: I’ve setup a function which returns a setInterval based Promise that updates and checks the screen every second producing a countdown. Once the first timer (work timer) reaches zero, I call the function again using ‘then’ for the rest period. This in its rudimentary way works fairly okay.
I now want to give my construct some properties as getters and setters so i can control the state of the timer such as pause, start, stop, reset, stuff like that. This is where I start getting errors I don’t know how to overcome.
Here is the relevant part of the code I am having issues with.
This works:
//setup construct
var Timer = function () {
var duration = 0;
this.setDuration = (num) => duration = num;
this.getDuration = () => duration;
}
//use my construct
var myTimer = new Timer();
myTimer.setDuration(9);
console.log(myTimer.getDuration());
when I run the code, I get 9. Perfect
This does not work:
//setup construct
var Timer = function () {
var duration = 0;
this.setDuration = (num) => duration = num;
this.getDuration = () => duration;
return new Promise(resolve => {
//do timer and screen update stuff here
resolve();
});
}
//use construct
var myTimer = new Timer();
myTimer.setDuration(9);
console.log(myTimer.getDuration());
when I run this code I get
TypeError: myTimer.setDuration is not a function
I know the issue is to do with how I am implementing or calling the lexical This. And I have a feeling it can be fixed with a bind, call, apply, or something of that sort. The thing is, I’ve been at this for three days and after reading a handful of tutorials, and YouTube videos I am not making any progress. I think I understand the basic concept but I don’t know how to apply it here.
Any help would be much appreciated
RockLobster7
PS. for the record; i know i can and have gotten the timer functionality working using global variables, but due to my compulsive pig-headedness, i really want to get this working as a construct / class.