[英]count how many times function was executed
I've website with image on background and i'm willing to make it move from right to left and i do not want to use jQuery so here is my code which makes it do that movement 我的网站上有背景图片,我愿意让它从右向左移动,我不想使用jQuery所以这里是我的代码,它使它做那个运动
HTML Code HTML代码
<div id="clouds_image"></div>
Javascript Code Javascript代码
var g = 0;
var speed=80;
var counter = 5;
function rollClouds()
{
document.getElementById('clouds_image').style.backgroundPosition=g+'px 0';
g--;
if (counter < 1)
clearInterval(interval);
}
interval = setInterval( function(){ rollClouds() }, speed)
This supposed to make the image moves from right to left and repeat its movement 5 times and counting down from 5 to 0 then will counter < 1
so it will execute clearInterval(interval)
to stop it. 这应该使图像从右向左移动并重复其移动5次并从5倒数到0然后将counter < 1
因此它将执行clearInterval(interval)
来停止它。
but i do not know why it keep repeating without stop ! 但我不知道为什么它不停地重复! so the code might have error so any idea how to make it stop after 5 times of repeating its movement. 所以代码可能有错误所以任何想法如何使其在重复其运动5次后停止。 ~ Thanks 〜谢谢
You don't seem to be decrementing the counter as your code implies you mean to do: 您似乎没有递减计数器,因为您的代码暗示您要执行的操作:
counter--;
Here's the full function: 这是完整的功能:
function rollClouds()
{
// decrement (decrease) the counter otherwise it's never less than 1
document.getElementById('clouds_image').style.backgroundPosition=g+'px 0';
g--;
if (counter < 1) {
clearInterval(interval);
}
counter--;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.