Having problems calling setTimeout with a parameter in the function

T

Terry

Hi folks.

I am calling setTimeout like this:

setTimeout(function()
{removeitem(document.getElementById(previous).getElementsByTagName('div')
);}, 1000);

and am getting an error message saying thestyle has no properties
within the error console of Firefox.

Can anyone help me with this?


The url is http://restaurant.atwebpages.com/menu.htm

Thanks,
Terry
 
D

David Golightly

Hi folks.

I am calling setTimeout like this:

setTimeout(function()
{removeitem(document.getElementById(previous).getElementsByTagName('div')
);}, 1000);

and am getting an error message saying thestyle has no properties
within the error console of Firefox.

Can anyone help me with this?

The url ishttp://restaurant.atwebpages.com/menu.htm

Thanks,
Terry


Hm, on your page I can't get past the fact that your page reloads
whenever I click on a link. You might try setting the value of "href"
to a "#", which will prevent this (failing setting it to a useful
alternate URL). If the "style" attribute has no properties, it's most
likely because you don't have a reference to a DOM node. The
"removeitem" function isn't getting what it needs. Do you know why
that might be? Have you accounted for the fact that perhaps your DOM
retrieval code isn't always going to get you a node?

-David
 
T

Terry

Hm, on your page I can't get past the fact that your page reloads
whenever I click on a link. You might try setting the value of "href"
to a "#", which will prevent this (failing setting it to a useful
alternate URL). If the "style" attribute has no properties, it's most
likely because you don't have a reference to a DOM node. The
"removeitem" function isn't getting what it needs. Do you know why
that might be? Have you accounted for the fact that perhaps your DOM
retrieval code isn't always going to get you a node?
I am not sure if this helps, but if I use the removeitem function by
itself (i.e not inside of setTimeout) it works.

Terry
 
R

RobG

Hi folks.

I am calling setTimeout like this:

setTimeout(function()
{removeitem(document.getElementById(previous).getElementsByTagName('div')
);}, 1000);


Your for loop goes around and sets all the setTimeout functions before
any of them runs. You have a closure back to the value of i (which is
global anyway because you didn't declare it) so by the time that the
setTimout functions run, i is -1 and you pass null to removeitem.

Break the closure and keep i local.

Once you fix that, you'll find some other errors. :)
 
T

Terry

Hi folks.
I am calling setTimeout like this:
setTimeout(function()
{removeitem(document.getElementById(previous).getElementsByTagName('div')
);}, 1000);


Your for loop goes around and sets all the setTimeout functions before
any of them runs. You have a closure back to the value of i (which is
global anyway because you didn't declare it) so by the time that the
setTimout functions run, i is -1 and you pass null to removeitem.

Break the closure and keep i local.


I am kind of new to this thing. Can you explain how I break this
closure? I think I know how to keep i local. I should declare it
within the function (i.e var i). I am not sure about this closure
thing though. Forgive my lack of knowledge.
Once you fix that, you'll find some other errors. :)

I am really looking forward to discovering the other errors.
 
T

Thomas 'PointedEars' Lahn

Terry said:
I am calling setTimeout like this:
setTimeout(function()
{removeitem(document.getElementById(previous).getElementsByTagName('div')
);}, 1000);

Your for loop goes around and sets all the setTimeout functions before
any of them runs. You have a closure back to the value of i (which is
global anyway because you didn't declare it) so by the time that the
setTimout functions run, i is -1 and you pass null to removeitem.

Break the closure and keep i local.


I am kind of new to this thing. Can you explain how I break this
closure? I think I know how to keep i local. I should declare it
within the function (i.e var i). I am not sure about this closure
thing though. Forgive my lack of knowledge.


Your problem is not exactly new and closures are explained
in the FAQ Notes.

http://jibbering.com/faq/


PointedEars
 
R

RobG

Hi folks.
I am calling setTimeout like this:
setTimeout(function()
{removeitem(document.getElementById(previous).getElementsByTagName('div')
);}, 1000);

Your for loop goes around and sets all the setTimeout functions before
any of them runs. You have a closure back to the value of i (which is
global anyway because you didn't declare it) so by the time that the
setTimout functions run, i is -1 and you pass null to removeitem.
Break the closure and keep i local.

I am kind of new to this thing. Can you explain how I break this
closure? I think I know how to keep i local. I should declare it
within the function (i.e var i).


Correct. :)
I am not sure about this closure
thing though. Forgive my lack of knowledge.

You might try this link:

<URL: http://www.jibbering.com/faq/faq_notes/closures.html >


There are also a number of post in the archives about closures.
 
T

Terry

You might try this link:

<URL:http://www.jibbering.com/faq/faq_notes/closures.html>

There are also a number of post in the archives about closures.

I have not tried to figure out the closure thing yet, but why won't
this work:


var i =
document.getElementById(previous).getElementsByTagName('div').length-1;

while (i>=0)
setTimeout(function()
{removeitem(document.getElementById(previous).getElementsByTagName('div')
[i--]);}, 1000);


Terry
 

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,769
Messages
2,569,582
Members
45,067
Latest member
HunterTere

Latest Threads

Top