ProgramKiddo ProgramKiddo - 1 month ago 16
ActionScript Question

Start and stop interval using button [as3]

I tried to make two buttons, one that starts an interval and one that stops it.
This is my code:

s_start.addEventListener(MouseEvent.CLICK, startspam);
function startspam(e:MouseEvent):void {
setInterval(spam,500);
}
s_stop.addEventListener(MouseEvent.CLICK, stopspam);
function stopspam(e:MouseEvent):void {
clearInterval(spam);
}


The start button works perfectly! but the stop button doesn't.
1067: Implicit coercion of a value of type Function to an unrelated type uint.

Thank you for your help in advance.

Answer

The clearInterval function accepts an unsigned integer which is an id to the interval you created not a function. Check out this tutorial for more info.

So you might want to try something like this

var intervalId:uint;

s_start.addEventListener(MouseEvent.CLICK, startspam);
function startspam(e:MouseEvent):void {
    intervalId = setInterval(spam,500);
}

s_stop.addEventListener(MouseEvent.CLICK, stopspam);
function stopspam(e:MouseEvent):void {
    clearInterval(intervalId);
}
Comments