繁体   English   中英

计算执行函数的次数

[英]count how many times function was executed

我的网站上有背景图片,我愿意让它从右向左移动,我不想使用jQuery所以这里是我的代码,它使它做那个运动

HTML代码

<div id="clouds_image"></div>

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)

这应该使图像从右向左移动并重复其移动5次并从5倒数到0然后将counter < 1因此它将执行clearInterval(interval)来停止它。

我不知道为什么它不停地重复! 所以代码可能有错误所以任何想法如何使其在重复其运动5​​次后停止。 〜谢谢

您似乎没有递减计数器,因为您的代码暗示您要执行的操作:

counter--;

这是完整的功能:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM