All,
I'm sorry but I still don't get this!!
I'm trying to use a variable that is passed into a function to to dynamically name an instance of an Object(). I've created a Timer() Object that seems to work just fine... the problem is I can't figure out how to pass in an instance name into setTimer() to create a new instance of Timer() object?? My ultimate goal is to have multiple instances of the Timer() Object running at once. While the code below works it only allows me to create 1 instance of the Timer() Object with the name of tmp.
I've tried adding both:
var tmp = divId; // This is what I would like...
var tmp = "tmp"; // But even this throws an error...
But these will throw a "tmp is undefined" error and points to line 1 chr 1? Unfortunately my knowledge of JavaScript is so limited I just don't see the problem?
Any help on this wold be greatly appreciated... Thanks in advance. - CES
function setTimer(status,divId){
//var tmp = divId; This will throws an error!!
if(status=="start"){
//var tmp = divId; This will throws an error!!
tmp = new Timer(divId); // Why will var tmp = new Timer(divId); throws an error here??
tmp.startTimer();
}else if(typeof tmp == "object"){
tmp.stopTimer();
tmp = ''; // Why does tmp = null; throw an error isn't object = null, the proper way of disposing of an object??
}
}
function Timer(name){
var secs = 10;
var timerID = null;
var timerRunning = false;
var delay = 1000;
if(typeof Timer.initialized == "undefined"){
Timer.prototype.startTimer = function (){
this.stopTimer();
this.counterStatus();
}
Timer.prototype.stopTimer = function (){
if(timerRunning != null){
clearTimeout(timerID);
this.timerRunning = false;
secs = 10;
}
}
Timer.prototype.counterStatus = function (){
if (secs==0){
this.stopTimer();
document.getElementById("id_" + name).style.backgroundColor = "green";
}else{
secs = secs - 1;
timerRunning = true;
document.getElementById("id_" + name).innerHTML = secs;
timerID = self.setTimeout(name + ".counterStatus()", delay);
}
}
Timer.initialized = true;
}
}
</script>
<div id="id_tmp" style="width:100px; height:100px; background-color:blue;" onmouseover="setTimer('','tmp')" onmouseout="setTimer('start','tmp')"" />