1
var direction;
function movement(){
    switch(direction){
        case 1: positionX = positionX + 10;
                break;
        case 2: positionX = positionX - 10;
                break;
        case 3: positionY = positionY - 10;
                break;
        case 4: positionY = positionY + 10;
                break;
    }
    snake.style.marginLeft = positionX + "px";
    snake.style.marginTop = positionY + "px";
}

function changeDirection(a){
    direction = a;
}

setInterval(movement(), 500);

I don't know why setInterval doesn't work. I tried only writing "funcName", funcName, funcName() and putting the function in a var but it doesn't work.

Hemsho
  • 21
  • 3

1 Answers1

2

setInterval receives callback as the first argument, but you're trying to call it with function result.
replace this:

setInterval(movement(), 500);

with this:

setInterval(movement, 500);
peter
  • 583
  • 2
  • 11