Passing values to my function :(

They have: 3 posts

Joined: Apr 2001

I was wondering if anybody can tell me why this script does not work.. I have tried to hack my way through troubleshooting the problem, and I still dont get it..

<script language="JavaScript1.2" type="text/javascript">

var number=0;
theElement="";
function moveIt(someValue)
{
number=number+19;if (number>someValue){number=0;return;}
eval(theElement).style.top = number;
setTimeout("moveIt(theElement)",8);
}
</script>

<div id="text1">This is the text I want to move!</div>

&lt;script&gt;theElement="text1";moveIt(200);&lt;/script&gt;
'
now, every time I call my function:
the element scrolls off the page and never stops! What gives? ALSO, I have to assign a value to my GLOBAL variable "theElement" instead of passing its value to my function! Passing a value to a function has to be the simplest thing you can do and yet, I cant do it! Why?

Any help would be greatly appreciated,

Tenke2x

detox's picture

They have: 571 posts

Joined: Feb 2001

are you wanting to trigger this onload?

try looking at

bratta

Plus the DHTMLIB 2.0 has all sorts of functions like this. I simply call from my hybrid version of that when I am trying to move divs everywhere.....

dhtml lib 2.0

They have: 3 posts

Joined: Apr 2001

I looked at your source for the animdemo (dont worry I only like to learn not steal!) and found something that I havent tried yet.

I have come to the conclusion that when I call setTimeout inside of a function, the variables used inside the function are destroyed. I noticed that your SlideCircles(num) function operates in the same way, and yet "num" doesnt lose its incremental value after you call setTimeout recirsively within your function! Why is this only happening in MY code?

HOWEVER, I also saw some code that went like this:

someVariable = setTimeout("someFunctionCall()", 40);

Does calling setTimeout this way preserve the values within the function that setTimeout was called in?

Thanks,

Tenke

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

If you declare your variables outside of the function, they should retain their values (as was done with the original post). If you declare your variables in the function, they will get reset. If you don't declare your variables, they will be null when you first use them (with every call to that function).

(sound right to everyone?)

Mark Hensler
If there is no answer on Google, then there is no question.

They have: 3 posts

Joined: Apr 2001

Thanks for your help! While I havent figured out a solution, you have helped me to come up with one (by using objects)..

Thanks again,

Tenke2x

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.