有一个脚本在需要更改时间间隔后更改div,因此它只运行一次

时间:2016-07-26 02:12:47

标签: javascript

我有以下js更改了div内容time interval,我喜欢它的工作方式,但需要对其进行更改,以便{I}只运行script这样做?

<script type="text/javascript">

function initChangeText()
{
    var time = 10;
    setInterval('changeText();',time*1000);
}
function changeText()
{
    var divs_ = document.getElementsByTagName("div")
    for (var i = 0;i<divs_.length;i++)
        if (divs_[i].className == "change")
            changeULText(divs_[i]);

}
function changeULText(obj)
{
    var ul = obj.getElementsByTagName("ul")[0];
    var li = obj.getElementsByTagName("li");
    for (var i=0;i<li.length;i++)
    {
        if (li[i].className == "show")
        {
            li[i].className = "";
            li[(i+1)%li.length].className = "show";
            return ;
        }
    }
}
window.onload = initChangeText;
</script>

由于 添

1 个答案:

答案 0 :(得分:2)

您可以使用setTimeout而不是setInterval     

function initChangeText(){
    var time = 10;
    setTimeout(changeText,time*1000);
}
function changeText(){
    var divs_ = document.getElementsByTagName("div")
    for (var i = 0;i<divs_.length;i++)
        if (divs_[i].className == "change")
            changeULText(divs_[i]);

    }
function changeULText(obj) {
    var ul = obj.getElementsByTagName("ul")[0];
    var li = obj.getElementsByTagName("li");
    for (var i=0;i<li.length;i++){
        if (li[i].className == "show"){
            li[i].className = "";
            li[(i+1)%li.length].className = "show";
            return ;
        }
    }
}
window.onload = initChangeText;
</script>