for some reason i can’t clear this interval with the ‘killTimer’ function. It works fine in the ‘switchTimer’ function…
any ideas?
code:
startTimer=function(){
clearInterval(mySI);
mySI=setInterval(switchTimer, 1000);
};
killTimer=function(){
trace("kill timer");
clearInterval(mySI);
}
counter=0;
switchTimer=function(){
trace("counter: "+counter);
if (counter == switchTime){
clearInterval(mySI);
switchIt();
counter=0;
}else{
counter++;
}
}
I don’t see where you created the interval initially and also don’t see the switchTime variable. :huh:
But the switchTime variable is what trigger the clearing of the interval in question.
yes, what i was trying to say is that the code he posted here was ok 
I can’t see anything wrong with the code in question, maybe you are calling the function wrong or something? Small typo somewhere?
If that is all the code you’ve got, you have to consider the scope of variables. By defining the timer in your startTimer function, does not mean it will be recognised in your killTimer function, it might be recognised in the switchTimer function since its being called from the timer, this sounds very unbelievable though.
If that is the only code then there are three reasons why this isn’t working
-
The killTimer function isn’t actually called anywhere (in which case I’m assuming you’re not seeing the trace in that function either)
-
as mentioned previously the timer variable is not declared outside the functions so has a local scope to the startTimer function - just put var mySi; above the startTimer function and the scope will be correct.
-
again, as mentioned previously the switchTime variable is undefined so the comparison operator in the switch Timer function will never execute as true
Hope this helps 
@valaran/carolinecook - the scope of the variable would be fine; variables are only restricted to the scope of a function when they’re given variable declarations and/or data types.
The proof:
function scopeTestWithDataType():Void {
var myVariable:String = "I have a data type";
}
scopeTestWithDataType();
trace(myVariable); // will return undefined
function scopeTestWithoutDataType():Void {
myVariable = "I have no data type";
}
scopeTestWithoutDataType();
trace(myVariable); // will return "I have no data type"
Back to the question. If you expect us to be able to help you can’t assume where the problem is. We need to be able to see the whole code, in context. As it is there is no evidence to say that:
a) switchTime is ever set
b) startTimer is ever called
c) killTimer is ever called
I don’t mean to hijack the thread, but thanks for the info Nathan, I had no idea about that.
when i define the variable outside the function it works:
var mySI:Number;
That is interesting…
function doTheInt():Void {
trace("allo");
}
function doInt():Void {
myInt = setInterval(doTheInt, 1000);
}
function wipeOutInt():Void {
trace("wiped interval 'myInt' -> type is: "+typeof(myInt))
clearInterval(myInt);
}
setInterval(wipeOutInt,5000);
doInt();
The above works fine and, essentially, does the same thing
also;
startTimer = function () {
clearInterval(mySI);
counter = 0;
mySI = setInterval(switchTimer, 1000);
};
killTimer = function () {
trace("kill timer");
clearInterval(mySI);
};
switchTimer = function () {
trace("counter: "+counter);
if (counter == switchTime) {
killTimer();
switchIt();
counter = 0;
} else {
counter++;
}
};
switchTime = 5;
startTimer();
which *is *your code works. Your problem is/was somewhere else…