stckpete
stckpete

Reputation: 571

setTimeout only runs once

I'm trying to add a delay to the cleatRect function, my aim is to make the animated sprites create a 'trailing effect' However, the setTimeout function only runs once when the canvas animation starts. Am I missing something obvious?

var draw = function() { // draw loop 
    setTimeout(function() { // this runs once
        ctx.clearRect(0, 0, 700, 560);
    }, 2000);

    ctx.drawImage(sprites.background, bkg.x, bkg.y);
    aliens.forEach(function(element, index) {
        ctx.drawImage(sprites.enemy, element.clipX, element.clipY, element.w, element.h, element.x, element.y, element.w, element.h);
    });
};

Upvotes: 0

Views: 547

Answers (1)

labue
labue

Reputation: 2623

setTimeout

Sets a timer which executes a function or specified piece of code once after the timer expires.

You want: setInterval

Repeatedly calls a function or executes a code snippet, with a fixed time delay between each call. Returns an intervalID.

Upvotes: 2

Related Questions