View Full Version : setTimeOut error:
TemperedSteel
08-17-2002, 05:58 PM
Hey Guys,
I am trying to have a sertain function fire randomly over and over again, here is what I have,
function loop(src) {
//actions
setTimeout("loop(" + src + ")", 25 +(400 * Math.random()) );
}
When it loads it works once, then I get an error,
Error: 'object' is undefined.
any ideas why, or another way I can acompish this same effect?
Thanks.
Try it this way
<script language="javascript">
<!--
function loop() {
//actions
timer=25+(Math.random()*400)
setTimeout("loop()", timer);
}
// -->
</script>
TemperedSteel
08-17-2002, 07:51 PM
I am not having trouble getting it to loop, it is that the pased variables wont carry over.
the time dosn't matter in this.
i.e.
function loop(){
setTimeout("loop()", 100)
}
that wont work, I need:
function loop(src){
setTimeout("loop(src)", 100)
}
but I cant get "src" to carry over.
brothercake
08-17-2002, 08:20 PM
you need to copy the variable; also you shouldn't use "src" as a custom variable name, because it's an attribute name.
var newSrc;
function loop(mySrc){
newSrc = mySrc;
setTimeout("loop(newSrc)", 100)
}
TemperedSteel
08-17-2002, 09:04 PM
ok that worked, thank you.
now is there a way for me to have several instances of the function going
I want it to loop through,
I have to images, onclick they start the looping I want them both to be changeing on each loop, but I click one and it loops, but when I click the other one it stops the first. any ideas?
TemperedSteel
08-17-2002, 09:23 PM
ok here is what I am trying to do,
I have an Array that hold all the names of my items that I want accccessed in my loop function,
MyArray = new Array("item1","item2")
function loop() {
RF = document.getElementsByName(MyArray[0])
RF.color = 'red';
RF = document.getElementsByName(MyArray[1])
RF.color = 'red';
}
any ideas why that wouldn't work, there are no error messages, just no color change. item1 and item 2 are font tags set with names item1 and item2.
ConfusedOfLife
08-18-2002, 06:17 AM
Hi, I couldn't understand what you meant by clicking on an image and it starts looping, may you write the whole code that I see what you mean?!
TemperedSteel
08-19-2002, 06:20 PM
This is not the whole code but a shortened version that is the same as the real thing.
<html>
<head>
<script language="JavaScript">
<!--
var MyArray = new Array("Item1", "Item2");
function loop1() {
for(i = 0; i = MyArray.length; i++) {
RF = document.getElementsByName(MyArray[i])
RF.color = 'red';
}
setTimeout("loop1()", 25 +(400 * Math.random()) );
}
function loop2() {
for(i = 0; i = MyArray.length; i++) {
RF = document.getElementsByName(MyArray[i])
RF.color = 'black';
}
setTimeout("loop2()", 25 +(400 * Math.random()) );
}
function loop(name) {
MyArray.length = MyArray.length + 1;
MyArray[MyArray.length] = name;
setTimeout("loop1()", 25 +(400 * Math.random()) );
setTimeout("loop2()", 25 +(400 * Math.random()) );
}
//-->
</script>
</head>
<body>
<font Name="item1" Id="item1">Flashing</font>
<font Name="item2" Id="item2">Text</font>
</body>
</html>
I hope that gives you a better idea.
beetle
08-19-2002, 06:50 PM
Well, I can see something wrong with your loops.for(i = 0; i = MyArray.length; i++) {You are acutally assigning the value of MyArray.length to i, which will break the loop. So, ditch the assignment operater for the equate operatorfor(i = 0; i == MyArray.length; i++) { However, this is still a problem. Remember, for loops loop until the condition set is NOT met. Since i is initialized to 0 and MyArray.length is 2, the condition is never met, hence the loop will never run. So, to be accurate, you needfor(i = 0; i < MyArray.length; i++) {If indeed, you want to apply the code within the loop to every element in MyArray, which could also be coded asfor (var i in MyArray) { Moving on...getElementsbyTagName returns an array, which you are not referencing properly. To do it right, you'd need RF = document.getElementsByName(MyArray[i]);
RF[0].style.color = 'red'; But, it's better to use getElementById(), especially since you've already assigned IDsRF = document.getElementByID(MyArray[i]);
RF.style.color = 'red';Also, what calls the function loop()?
beetle
08-19-2002, 06:55 PM
Oh, and one more thing. To catch up with standards (a bit) you should 86 the FONT objects for SPANs and use RF.style.color
beetle
08-19-2002, 06:57 PM
Oh, and ONE MORE thing (promise :D) you don't really need two loops to achieve what you are doing here. I can show you if you like.
ConfusedOfLife
08-19-2002, 08:36 PM
First of all I agree with whatever dear beetle said, and I think the analysis was something more than enough that it's no need that I say anything else ( but I will!), to be frank, I still can not understand what you are EXACTLY looking for. explain what you want and also work on what beetle told you, and I'm sure that you can make it yourself, also you can explain more clearly that we find out what you're looking for. I wrote something that I thought might be near to what you want ( also it's very ugly! ), but just take a look at it and tell me how close I was!
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script>
function something()
{
first = Math.floor(Math.random() * 90);
second = Math.floor(Math.random() * 90);
third = Math.floor(Math.random() * 90);
first = ( first <= 9 ) ? ( "0" + first ) : first;
second = ( second <= 9 ) ? ( "0" + second ) : second;
third = ( third <= 9 ) ? ( "0" + third ) : third;
document.getElementById('item1').style.color = "#" + first + second + third;
setTimeout("something()", 1000);
}
</script>
</head>
<body onload="something()">
<p style="color : black" id="item1"> this is just a test! </p>
</body>
</html>
TemperedSteel
08-19-2002, 10:21 PM
Thanks guys, that returning an array thing is what had me.
The only thing that is not good with your Random Color generator is that all the colors are fark and kind of pastel. becasue they are not able to get the higher set aa-ff, and those are the colors I want, I want aa-ff not 00-99, so if yuo have any other ideas, on how to change the random number to hex, I would appreacite it
Thank You, TemperedSteel.
beetle
08-19-2002, 10:41 PM
yes!first = Math.floor(Math.random() * 255);
second = Math.floor(Math.random() * 255);
third = Math.floor(Math.random() * 255);
first = ( first <= 9 ) ? ( "0" + first ) : first;
second = ( second <= 9 ) ? ( "0" + second ) : second;
third = ( third <= 9 ) ? ( "0" + third ) : third;
first = first.toString(16);
second = second.toString(16);
third = third.toString(16);
TemperedSteel
08-19-2002, 10:42 PM
Hey guys now that I got the loop going I cant get the Array to work right, I am trying to add objects to it.
var FO = new Array("LN", "LN2");
function AddItem(ObjectName) {
FO[FO.length + 1] = ObjectName;
}
is there any reason that this wouldn't work?
beetle
08-19-2002, 10:46 PM
Yes, and this is why. Arrays are zero-based. In other words, their first index is 0, NOT 1. So in an array that has a length of 2, it's 2nd (and last) index is 1. So, ditch the '+1' and you are golden.
ConfusedOfLife
08-21-2002, 07:22 PM
Well, I don't know what you're really trying to write, but I know that the thing that you wrote doesn't work the way that you expect! Because <<anArray.length>> will always give you the length of something in the human's number! I mean if you have
anArray[0], anArray[1], anArray[2]
then "anArray.length = 3", you got it?
means that in here you should write:
function AddItem(ObjectName) {
FO[FO.length] = ObjectName;
}
Hope that it works...
ConfusedOfLife
08-21-2002, 07:39 PM
Now I have a question from dear beetle! I ran your code and after several times of running ( I mean excecuting our setTimeout), it gave me an error message in the line that we wana change the color, I mean:
document.getElementById('item1').style.color = "#" + first + second + third;
so, I traced the bug and I saw that it's a number like "a4e61" that cause the bug, and it's right, I mean as you notice it's a 5 digits number and it's illegal! it seems that it somehow escapes our conditions that if the number is less than 9, then add "0" to its begining, I changed the place of our conditions and put them after your new red lines ( I mean toString(16) ) and it didn't work either, what do you suggest?!
Besides, in order to find that bug, I put an alert before the line that changes the color of our text, and it took me a long time to find the bad number, coz you know that each time it alerts and then changes the color, is it a way that I don't put the alert to find out where the bug is? I mean a kind of "onerror" maybe! event handler for the errors?
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.