I have two buttons
. The one onClick
runs a function that uses a var intId = setInterval{function(), 30}
On the second button
i try to stop the setInterval with clearInterval(intId)
but intId
is not global virable and if i put the whole setInterval outside of the function of the button it can't run.
run button
var intID == 0;
function runButton() {
var c = document.getElementById("can1");
var ctx = c.getContext("2d");
var speed = 2;
var posX = 20;
var posY = 20;
var intID = setInterval(function() {
posX += speed;
ctx.clearRect(0, 0, c.width, c.height);
ctx.fillStyle = "black";
ctx.fillRect(posX, posY, 20, 20);
if (intID == 1) {
clearInterval(intID);
}
}
, 30);
}
stop button
function stopButton() {
var c = document.getElementById("can1");
var ctx = c.getContext("2d");
clearInterval(intID);
intID == 1;
ctx.clearRect(0, 0, c.width, c.height);
c.style.backgroundColor = red;
}