0
votes

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.

1
Welcome to Stackoverflow. You have a correct answer. Please use the big "correct" tick to mark this question as solved & to show thanks. - VC.One
Why did I get a -1 on this post lol - ProgramKiddo
Doesn't make sense so I've done a cancelling +1. I suspect that since coders who write programs to spam are not nice people, maybe that's why you got a random negative reaction?... - VC.One
Thank you :P Well, it was not intended to harm someone, just to "spam" the function. Maybe I should have called it repeat - ProgramKiddo

1 Answers

1
votes

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