This content has been marked as final. Show 13 replies
you really shouldn't put strings (like "12" etc) in an array where you expect numbers. setInterval() will use strings so it works in your current code, but that's (accepting a string when a number is expected) a rarity in flash.
anyway, if you want to repeatedly execute fRandomCall() at random intervals of 12, 14 , 18 etc milliseconds use:
You don't seem to be sending the name of a movieclip instance to your function with the setInterval. Could that be the problem. As for the other part of your question…
So what is not to understand? setInterval(function, time delay, arguments) means to start calling function ever time delay and pass the function those arguments (if any). The interval will continue to call, over and over, until you clearInterval().
Each time you setInterval, Flash keeps track of it internally. The first interval that it tracks is 1, then 2, and so on. That number is "returned" by the setInterval call so you can assign it to a variable is you want.
Will keep track of the interval in the variable myInterval and show you that it is just a number. Components and some other Flash things use intervals in their code – stuff you never see – so you can't count on your intervals having consecutive numbers. Sometimes they will be and sometimes they will skip. You can count on the numbers always getting larger, but you won't know by how much.
So if you do something like this:
You will see that the value of myInterval will be set twice with different values. But the variable isn't the interval, it is just its number. Like when you go to a bakery and they give you a number. It isn't you or the delicious baked goods, it is just a slip of paper with a number that says your place in line. So both intervals will keep going and continually operating until cleared. But here is the problem.
In the above example, you will "lose" the first value – let's say in this example the first value was 12 and the next value was 21. Since 12 was replaced by 21, the following line:
will clear the interval that is identified by 21 – the someFunction2. However there is no record (available to you) that interval number 12 is doing someFunction1 every 100 milliseconds. Now if you knew, you could do this:
and that would stop it. But how are you going to know that it is 12 and not 74 or any other of a million possible values?
Oops, so I should set up the array as numbers instead of strings. I see my mistake there. Instead of calling that variable inside of the function repeatedly, is there a way to step through the array to plug in the next interval time? Also, I'm trying to set these intervals as "seconds" so I realize I need to multiply it by 1000 at some point. I assume it would be as follows:
I realize now why they told me I'd use math someday. I should have focused more in school. :-S
Thanks, Rothrock, for your detailed explanation. I understand the basic concept of setInterval but it gets crazy for me when I start defining it as a variable and you've helped me understand that better. Now, if I were to take your example and change the variable names to myInterval1 and myInterval2, does the fact remain that one will "overwrite" the other? Will it still be an issue to clear it? I would just "clearInterval(myInterval1);" and "clearInterval(myInterval2);" correct? Was that the point you were trying to make about that... that I need to establish various intervals with their own variable names so I can clear them properly??
Thanks for your patience and help.
Remember you aren't really defining the interval as a variable. The interval is defined and kept track of by the Flash player, you are just keep a record of the intervals ID number as it were.
You seem to understand it. If the variables have different names then they won't overwrite each other. Each variable can only hold one thing. So if you try and put another ID number in it, the first is lost.
Yup that would more or less be my point.
And of course there is no problem of "reusing" a variable if you have already cleared the interval.
If you are going to have many intervals, you might just want to push() them all on to an array.
kglad, this code sends the player into an endless loop. I have tried something else so that another interval is used but my method has failed. My thought was to create a new interval, clear the first one and start the new function. I hope I can wrap my head around this soon but you guys are certainly helping me get there.
i thought you wanted an endless loop with fRandomCAll() being executed between 12 and 36 seconds (not milliseconds). what is it that you want to occur?
Geez, I hope I don't post twice with this reply... the forum's acting up on me. Anyway...
Sorry for not being more clear. What I want to do is call the fRandomCall function at random intervals. Not a repeating interval from a list of random numbers. For instance I want it to occur first after 10 seconds, then after 8 seconds, then after 30, then after 18... whatever the number (but pulled from an array)... just so it appears to happen randomly instead of after the same amount of time.
What I meant by the "endless loop" was that it locked up Flash Player in testing mode and I had to force quit Flash to clear it.
Ah, well the point of each setInterval is that it will repeat at the rate (more or less) set. But no problem, this just means you will use each interval once, clear it, and then set another. Now without the array part.
//do some other stuff
wow! Just as I suspected.. I make things more difficult for myself than necessary. Thanks a billion, that makes so much more sense!
then the code i gave is what you wanted except you should convert from milliseconds to seconds by multiplying by 1000.
OK, great! Thanks for all your help. I hope I can be as helpful to others someday.