Clearing an interval
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