name _F1
04-19-2007, 06:16 AM
I'm just learning object-oriented Javascript, and I decided to try making some timers with it.
My object is below, heavily commented for your benefit.
function timer() {
this.startMins;
this.startSecs; //The time to start the timer at
this.mins;
this.secs; //The current amount of time displayed
this.action; //1 for count-up timers, -1 for count-down
this.msg; //Message to display when count-down reaches 0
this.prefix; //Prefix for IDs of timers
this.stopBtn = document.getElementById(this.prefix + "Stop");
this.startBtn = document.getElementById(this.prefix + "Start");
this.resetBtn = document.getElementById(this.prefix + "Reset");
this.display = document.getElementById(this.prefix + "Display");
this.intervalId; //Identifier for the interval to update timer
this.start = function() {
this.startBtn.disabled=1;
this.stopBtn.disabled=0;
this.resetBtn.disabled=0; //Disable necessary buttons
this.intervalId = setInterval("this.count()",1000); //Start timer
}
this.count = function() {
eval(this.secs += this.action); //Count up or down
if (this.secs >= 60) {
this.secs = 0;
this.mins++; //Convert to minutes/seconds
}
if (this.secs < 0) {
if (this.mins > 0) {
this.secs = 59;
this.mins--;
}
else {
alert(this.msg); //Alert msg if count-down is at 0
}
}
this.displayOutput();
}
this.stop = function() {
clearInterval(intervalId); //Stop timer
this.startBtn.disabled=0;
this.stopBtn.disabled=1; //Disable buttons
}
this.reset = function() }
this.startBtn.disabled=0;
this.stopBtn.disabled=1;
this.resetBtn.disabled=1;
this.mins = this.startMins;
this.secs = this.startSecs; //Reset timer to initial values
this.displayOutput(); //Update display to reflect changes
}
this.displayOutput = function() {
this.display.innerHTML = this.mins+":"+this.secs; //Display time
}
}
I'm unsure what I need to do next. Please read over my code, ensure that it is well-written before I develop any bad habits. Also, performance is the most important aspect of this object, so I would like to optimise it for performance as much as possible.
I've basically done all I can do on that object from the knowledge I have, and I believe that is the main code that I need. I'm not sure how I would go about making an actual object to use on my page.
The idea was that I could have HTML forms on my page that showed the timers.
<span id="xxDisplay"></span>
<input type="text" id="xxStop" value="Stop" />
<input type="text" id="xxStart" value="Start" />
<input type="text" id="xxReset" value="Reset" />
Notice that the prefix in this case would be "xx". Then I would initialise the object, and set all necessary values.
My object is below, heavily commented for your benefit.
function timer() {
this.startMins;
this.startSecs; //The time to start the timer at
this.mins;
this.secs; //The current amount of time displayed
this.action; //1 for count-up timers, -1 for count-down
this.msg; //Message to display when count-down reaches 0
this.prefix; //Prefix for IDs of timers
this.stopBtn = document.getElementById(this.prefix + "Stop");
this.startBtn = document.getElementById(this.prefix + "Start");
this.resetBtn = document.getElementById(this.prefix + "Reset");
this.display = document.getElementById(this.prefix + "Display");
this.intervalId; //Identifier for the interval to update timer
this.start = function() {
this.startBtn.disabled=1;
this.stopBtn.disabled=0;
this.resetBtn.disabled=0; //Disable necessary buttons
this.intervalId = setInterval("this.count()",1000); //Start timer
}
this.count = function() {
eval(this.secs += this.action); //Count up or down
if (this.secs >= 60) {
this.secs = 0;
this.mins++; //Convert to minutes/seconds
}
if (this.secs < 0) {
if (this.mins > 0) {
this.secs = 59;
this.mins--;
}
else {
alert(this.msg); //Alert msg if count-down is at 0
}
}
this.displayOutput();
}
this.stop = function() {
clearInterval(intervalId); //Stop timer
this.startBtn.disabled=0;
this.stopBtn.disabled=1; //Disable buttons
}
this.reset = function() }
this.startBtn.disabled=0;
this.stopBtn.disabled=1;
this.resetBtn.disabled=1;
this.mins = this.startMins;
this.secs = this.startSecs; //Reset timer to initial values
this.displayOutput(); //Update display to reflect changes
}
this.displayOutput = function() {
this.display.innerHTML = this.mins+":"+this.secs; //Display time
}
}
I'm unsure what I need to do next. Please read over my code, ensure that it is well-written before I develop any bad habits. Also, performance is the most important aspect of this object, so I would like to optimise it for performance as much as possible.
I've basically done all I can do on that object from the knowledge I have, and I believe that is the main code that I need. I'm not sure how I would go about making an actual object to use on my page.
The idea was that I could have HTML forms on my page that showed the timers.
<span id="xxDisplay"></span>
<input type="text" id="xxStop" value="Stop" />
<input type="text" id="xxStart" value="Start" />
<input type="text" id="xxReset" value="Reset" />
Notice that the prefix in this case would be "xx". Then I would initialise the object, and set all necessary values.