gpt4 book ai didi

javascript - setTimeout 只运行一次

转载 作者:行者123 更新时间:2023-12-02 14:11:19 24 4
gpt4 key购买 nike

我正在尝试向 cleatRect 函数添加延迟,我的目标是让动画 Sprite 创建“拖尾效果”,但是,仅 setTimeout 函数当 Canvas 动画开始时运行一次。我是否遗漏了一些明显的东西?

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);
});
};

最佳答案

setTimeout

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

您想要:setInterval

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

关于javascript - setTimeout 只运行一次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39536177/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com