$(document).ready(function() { console.log("ready"); var snake = $("#snake"); var fruit = $("#fruit"); function drawFruit(){ var xFruit = Math.floor((Math.random()*10)+1)*4; var yFruit = Math.floor((Math.random()*10)+1)*4; var positionFruit = [xFruit,yFruit]; return positionFruit; } function moveSnake(){ snake.css({ 'left' : "+=1rem", }) } function startGame(){ var myVar = setInterval(moveSnake(),3000); } for(var i = 0; i !==5; i++){ startGame(); } var newPositionFruit = drawFruit(); fruit.css({ 'left': newPositionFruit[0] + 'rem', 'top': newPositionFruit[1] + 'rem', }) // moveSnake(); })