Skip to main content
Participant
April 20, 2010
Answered

Clearing an interval

  • April 20, 2010
  • 1 reply
  • 441 views

I have a small game I am developing. To mimic levels I have a series of setIntervals which run at different times. They look like this:

var level1Timer = setInterval(generateEnemies, 2000, 2, 100, randomSpeed + 3, 0xccccff, "level1Timer");

The parameters pass into a function which looks like this:

function generateEnemies(noEn, trans, speed, color, level)

The point in question is the last parameter, level. The reason I am passing this through is so that after the interval has run, I can then clear it so that it does not keep looping. To do this I am using just

clearInterval(level);

However this does not work. During some error testing, I noticed that if I passed in the parameter 'level1Timer' BUT WITHOUT quotes around, displaying the value showed undefined. As I am still learning, I can only assume this happens because without quotes it attempts to pass through a variable called level1Timer, and not the value as a string. Hence, this is why I have put quotes around now and this does indeed display as normal. Problem now is it does not seem to clear, possibly because it has been converted into a string??

I know the clear definitely works as if I put in:

clearInterval(level1Timer);

it works.

Hope someone can help,

Chris

This topic has been closed for replies.
Correct answer kglad

use the array operator to have flash coerce your string to an object.

var tl:MovieClip=this;

function generateEnemies(noEn, trans, speed, color, level){

clearInterval(tl[level])

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
April 20, 2010

use the array operator to have flash coerce your string to an object.

var tl:MovieClip=this;

function generateEnemies(noEn, trans, speed, color, level){

clearInterval(tl[level])

Grif_22Author
Participant
April 20, 2010

Perfect. Works a treat!

Don't suppose you could explain that slightly more though could you? I didn't really understand this:

"have flash coerce your string to an object"

Just for future reference!

Many thanks,

Chris

kglad
Community Expert
Community Expert
April 20, 2010

your level parameter is a string.  it's not a variable.  flash is too stupid to realize you want it (flash) to be treated like a variable.  you can instruct flash to treat it like a variable by using the array operator scopeobject(a left bracket, your string, right bracket).

the scopeobject is the scope in which your variable is defined.  your variable is defined in the timeline where you have that code:

var tl:MovieClip=this;  // tl gives a reference to the scope of your variable

var yourvariable:Number = setInterval(whatever,...)

tl["yourvariable"]  is a reference to the variable, yourvariable

"yourvariable" is just a string to flash.  it doesn't see any similarity between "yourvariable" and yourvariable.  those two things look like an apple and an orange to flash.  but when you use:

tl["yourvariable"]

flash sees tl.yourvariable (assuming such a thing exists).