Workshop › Snake › Step 9 of 28
The square is far too fast. We still draw every frame, but move only every 150 milliseconds.
```js
const SPEED = 150 // milliseconds between moves
let last = 0
function loop(time) {
if (time - last >= SPEED) {
last = time
update()
}
draw()
requestAnimationFrame(loop)
}
```SPEED is the wait between moves; last remembers when we last moved.loop(time) receives the current time in milliseconds from the browser.if (time - last >= SPEED) is true only when enough time passed; then we note the time and move.const CELL = 20 write the SPEED line; under let head = ... write let last = 0.loop as shown: time in the parentheses and update() inside the if.Skills: The game loop
← The game loop A direction to move in →