JavaScript setTimeout和setInterval

时间:2013-11-17 06:14:21

标签: javascript html

我有三个div,应该无限次地出现一个。我需要在能见度变化之间间隔2秒。这就是我想出的。但它不起作用。请帮助我让它工作

var taglines = document.getElementsByClassName("tagline");
taglines[0].style.color = 'red';
setInterval(function () {
taglines[1].style.visibility = 'hidden';
taglines[2].style.visibility = 'hidden';
    setTimeout(function () {
        taglines[1].style.visibility = 'visible';
        taglines[0].style.visibility = 'hidden';
        taglines[2].style.visibility = 'hidden';
    }, 2000);
    setTimeout(function () {
        taglines[2].style.visibility = 'visible';
        taglines[0].style.visibility = 'hidden';
        taglines[1].style.visibility = 'hidden';
    }, 4000);
}, 5000);

1 个答案:

答案 0 :(得分:0)

我会推荐CSS,但是如果你真的想做javaScript,你会这样做

var taglines = document.getElementsByClassName("tagline");
curTag = 0;

setInterval(function(){
    taglines[curTag].style.visibility = 'hidden';
    curTag++;
    if (curTag==3) { curTag=0;}
    taglines[curTag].style.visibility = 'visible';
},2000);

http://jsfiddle.net/fPDfv/