473,386 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

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

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>
Nov 30 '05 #1
1 1428
CES wrote:
//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.
x = '';


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
Nov 30 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Terry | last post by:
I have a number of input boxes used to display totals based on selected items for each row in a table. There are more than a few rows that are identical, except for the form field name. I have...
2
by: Adi | last post by:
Okay, this issue has been annoying me for a little while now. I need it to work on Mozilla 1.6 & IE 6 This is what I would like to be able to do: var row = document.createElement("TR"); var...
16
by: sirsean | last post by:
Hi all. I'm trying to dynamically build menus and objects after my page loads. Data is stored in an XML file and is parsed at runtime into Javascript objects. At the moment, I'm working on creating...
39
by: Randell D. | last post by:
Folks, I'm sure this can be done legally, and not thru tricks of the trade - I hope someone can help. I'm writing a 'tool' (a function) which can be used generically in any of my projects. ...
8
by: Peter B. Steiger | last post by:
The latest project in my ongoing quest to evolve my brain from Pascal to C is a simple word game that involves stringing together random lists of words. In the Pascal version the whole array was...
5
by: Fresh Air Rider | last post by:
Hello Could anyone please explain how I can pass more than one arguement/parameter value to a function using <asp:linkbutton> or is this a major shortfall of the language ? Consider the...
1
by: Kum | last post by:
Hi, I need help in asp.net dynamic textbox controls validation. I am creating textbox controls dynamically on a asp.net webpage. Now after creating the textboxes on the page I want to validate...
5
by: tricard | last post by:
Good day all, I have created a two dimensional array (matrix for my purposes) whose size is dynamically allocated. (i.e. rowSize and colSize are both taken as input, then malloc() is used to...
36
by: Pat | last post by:
Hi, I've run into a strange problem, but one that seems like it might be fairly common. I have a single base class, from which several other classes are derived. To keep the example simple,...
1
by: johnjsforum | last post by:
Buddies, I have a web page to create HTML buttons dynamically as in the “formDivColorPicker” function ////////////////////////////////////////////////////////////////////////////////////...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.