Why can't I dynamically pass a var into this function?

C

CES

All,

I have absolutely no clue as to what the difference is between these two snippets but the on that is hard coded works and the other doesn't. I know it's something incredible stupid that I should know but I just cant see it.

//This works if it's hard coded:
function setTimer(id,status){
if(status=="start"){
txt = new timer('txt',id);
txt.startTimer();
}else if(typeof txt == "object"){
txt.stopTimer();
txt = '';
}
}

//This doesn't work
function setTimer(id,status){
var x = txt;
if(status=="start"){
x = new timer(x,id);
x.startTimer();
}else if(typeof txt == "object"){
x.stopTimer();
x = '';
}
}

Any help on this would be greatly appreciated. Thanks in advance. - CES

The rest of the page is as follows:

function timer(name,id){
var secs = 10;
var timerID = null;
var timerRunning = false;
var delay = 1000;

this.startTimer = function (){
this.stopTimer();
this.counterStatus();
}
this.stopTimer = function (){
if(timerRunning != null){
clearTimeout(timerID);
this.timerRunning = false;
secs = 10;
}
}
this.resetTimer = function (){
stopTimer();
}
this.counterStatus = function (){
if (secs==0){
this.stopTimer();
document.getElementById(id).style.backgroundColor = "Red";
}else{
secs = secs - 1;
timerRunning = true;
document.getElementById(id).innerHTML = secs;
timerID = self.setTimeout(name + ".counterStatus()", delay);
}
}
}


<div id="id_test" style="width:100px; height:100px; background-color:blue;" onmouseover="setTimer('','')" onmouseout="setTimer('id_test','start')">
</div>
 
T

Thomas 'PointedEars' Lahn

CES said:
//This works if it's hard coded:
function setTimer(id,status){
if(status=="start"){
txt = new timer('txt',id);
txt.startTimer();
}else if(typeof txt == "object"){
txt.stopTimer();
txt = '';
}
}

//This doesn't work
function setTimer(id,status){
var x = txt;

So you store the value of the variable or property `txt' into x.
Since the variable or property was not declared and not defined,
the value stored is `undefined'.
if(status=="start"){
x = new timer(x,id);

Then you overwrite the value of `x' (`undefined') with a reference to
a newly created `timer' object. [The first argument of the constructor
call, which strangely is (still) `undefined', is used in the constructor
in

timerID = self.setTimeout(name + ".counterStatus()", delay);

where `name' represents the value of the first argument, that, due to
automatic type conversion caused by string concatenation, is converted
into the string value "undefined". This will result in

timerID = self.setTimeout("undefined.counterStatus()", delay);

which sets

undefined.counterStatus()

to be executed after delay milliseconds. If supported, `undefined' is a
property of the global object. However, the `undefined' value it refers
to in that case is not an object, hence does not have a counterStatus()
property. So the method call will fail with a ReferenceError.]
x.startTimer();

and call the method startTimer() of that object
}else if(typeof txt == "object"){
x.stopTimer();

or try to call the stopTimer() method of its value `undefined', which it
does not have. However, since the value of `txt' is always `undefined',
this branch will never be executed.

And then you overwrite the value of `x' with the empty string.


Debugging of any kind would have showed all this before posting.

<URL:http://jibbering.com/faq/#FAQ4_43>


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top