ProgramKiddo
ProgramKiddo

Reputation: 333

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, startRepeater);
s_stop.addEventListener(MouseEvent.CLICK, stopRepeater);

function startRepeater(e:MouseEvent) : void {
setInterval(repeater,500);
}

function stopRepeater(e:MouseEvent) : void {
clearInterval(repeater);
}

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.

Upvotes: 0

Views: 192

Answers (1)

davidejones
davidejones

Reputation: 1949

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);
}

Upvotes: 1

Related Questions