[英]How can I add a 5 second delay to this setInterval based sprite animation?
I have a sprite which runs using the setInterval method, It runs all the time and moves the css background position (x) by 100/(number of images -1) every interval of 60ms. 我有一个使用setInterval方法运行的sprite,它始终运行,每60ms的间隔将css背景位置(x)移动100 /(图像数量-1)。 When the position hits 96% i reset it to 0. Simple.
当位置达到96%时,我将其重置为0。很简单。 This is the formula for animating a sprite with percentages.
这是用百分比动画精灵的公式。
Now I just want to add a 5 second delay between each time it runs (every time it hits 96% x position, wait 5 seconds and then run again). 现在,我只想在每次运行之间添加5秒的延迟(每次达到96%x位置时,等待5秒钟然后再次运行)。 What is the easiest way to achieve this.. I tried wrapping the setInterval in another set interval but the problem here is that only runs it more often (and makes it go crazy).
实现此目的最简单的方法是什么。我尝试将setInterval包装在另一个set间隔中,但是这里的问题是,它只会更频繁地运行(并使它发疯)。 I know there is also a method called
clearInterval
and i am thinking maybe using that every few seconds would work but then how do i restart the animation afterwards?? 我知道还有一种方法称为
clearInterval
,我在想也许每隔几秒钟就可以使用,但是之后如何重新启动动画呢?? I need it running over and over with a 5 second delay in between each run. 我需要它一遍又一遍地运行,每次运行之间要有5秒的延迟。
function animateAlways() {
var positionHeadDot = 0;
var interval = 60;
const diffHeadDot = 3.703704;
shadeSparkle = setInterval(() => {
/////////////////////HeadDot////////////////////////////////////
document.getElementById("imageHeadDot").style.backgroundPosition =
`${positionHeadDot}% 0%`;
if (positionHeadDot < 96) {
positionHeadDot = positionHeadDot + diffHeadDot;
}
else {
positionHeadDot = 0;
}
}, interval);
}
animateAlways()
Probably it is easier when you convert your setInterval
code to use setTimeout
. 当您将
setInterval
代码转换为使用setTimeout
时,可能会更容易。 Then you can distinguish between the progress and final step and provide an adjusted timeout value: 然后,您可以区分进度和最后一步,并提供调整后的超时值:
function animateAlways() {
var positionHeadDot = 0;
var interval = 60;
var delay = 5000;
const diffHeadDot = 3.703704;
function animate() {
/////////////////////HeadDot////////////////////////////////////
document.getElementById("imageHeadDot").style.backgroundPosition =
`${positionHeadDot}% 0%`;
if (positionHeadDot < 96) {
positionHeadDot = positionHeadDot + diffHeadDot;
setTimeout(animate, interval);
}
else {
positionHeadDot = 0;
setTimeout(animate, delay); // 5 second wait.
}
}
animate();
}
You could add an outer setInterval
, then use clearInterval
to clear the inner interval instead of setting the position back to 0: 您可以添加一个外部
setInterval
,然后使用clearInterval
清除内部间隔,而不是将位置设置回0:
function animateAlways() { var positionHeadDot = 0; var interval = 60; const diffHeadDot = 3.703704; shadeSparkle = setInterval(() => { /////////////////////HeadDot//////////////////////////////////// document.getElementById("imageHeadDot").style.backgroundPosition = `${positionHeadDot}% 0%`; if (positionHeadDot < 96) { positionHeadDot = positionHeadDot + diffHeadDot; } else { clearInterval(shadeSparkle); } }, interval); } animateAlways(); setInterval(animateAlways, 5000);
#imageHeadDot { background-image: url('https://www.w3schools.com/CSSref/smiley.gif'); width: 600px; height: 50px; border: 3px solid black; }
<div id="imageHeadDot"></div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.