使用按钮 [as3] 开始和停止间隔

Start and stop interval using button [as3]

我试着做了两个按钮,一个开始间隔,一个停止间隔。 这是我的代码:

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

开始按钮完美运行!但停止按钮没有。 1067:将 Function 类型的值隐式强制转换为不相关的 uint 类型。

提前感谢您的帮助。

clearInterval 函数接受一个无符号整数,它是您创建的间隔的 ID,而不是函数。查看此 tutorial 了解更多信息。

所以你可能想尝试这样的事情

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