-2

我想制作一个java脚本,它每5秒自动循环一次我的函数这是我的代码

function showlisten() {
   document.getElementById('listen').style.display = "block";
   document.getElementById('review').style.display = "none";
   document.getElementById('earn').style.display = "none";
   document.getElementById('listen-text').style.display = "block";
   document.getElementById('review-text').style.display = "none";
   document.getElementById('earn-text').style.display = "none";
}
function showreview() {
   document.getElementById('listen').style.display = "none";
   document.getElementById('review').style.display = "block";
   document.getElementById('earn').style.display = "none";
   document.getElementById('listen-text').style.display = "none";
   document.getElementById('review-text').style.display = "block";
   document.getElementById('earn-text').style.display = "none";
}
function showearn() {
   document.getElementById('listen').style.display = "none";
   document.getElementById('review').style.display = "none";
   document.getElementById('earn').style.display = "block";
   document.getElementById('listen-text').style.display = "none";
   document.getElementById('review-text').style.display = "none";
   document.getElementById('earn-text').style.display = "block";
}
4

4 回答 4

4

这个很简单。

var clearToken = window.setInterval(function () {
    showlisten(); showreview(); showearn();
}, 5000);

感谢 QuickSilver 博士发现了需求的真实性质。更新如下:

function showlisten(){
    document.write("listen<br />");
}
function showreview(){
    document.write("review<br />");
}
function showearn(){
    document.write("earn<br />");
}

showlisten();
window.setInterval(function () {
    showlisten();
}, 15000);
window.setTimeout(function () {
    showreview();
    window.setInterval(function(){
        showreview();        
    }, 15000);
}, 5000);
window.setTimeout(function () {
    showearn();
    window.setInterval(function () {
        showearn();
    }, 15000);
}, 10000);

我更喜欢 QuickSilver 的方法,但复制样式将是多余的,因此我采用了不同的方法来为您提供两种解决方法。

我为它创建了一个小提琴,但该网站现在似乎正在为我播放:http: //jsfiddle.net/jdjE5/希望该链接对您有用。

于 2013-04-04T20:54:29.163 回答
1

你想让它处于'listen'状态5秒,然后'review'状态5秒,然后'earn'状态5秒,然后循环吗?

如果是这样,您需要 3 个 setTimeouts 链:

function dolisten() {
   showlisten();
   setTimeout(doreview,5000);
}
function doreview() {
   showreview();
   setTimeout(doearn,5000);
}
function doearn() {
   showearn();
   setTimeout(dolisten,5000);
}

你也可能setIntervals在 15 秒的时钟上有 3 个不相关的,但我不确定它们是否会保持同步,而且这并不简单。

于 2013-04-09T06:23:21.183 回答
0

你正在寻找 setInterval() ;)

于 2013-04-04T20:53:42.287 回答
-1

我应该缓存 DOM 元素,例如:

function loopFunctions(){
   var elemListen = document.getElementById('listen'),
      elemReview = document.getElementById('review'),
      elemEarn = document.getElementById('earn'),
      elemListenText = document.getElementById('listen-text'),
      elemReviewText = document.getElementById('review-text'),
      elemEarnText = document.getElementById('earn-text'),
      functions = [
         function(){
            elemListen.style.display = "block";
            elemReview.style.display = "none";
            elemEarn.style.display = "none";
            elemListenText.style.display = "block";
            elemReviewText.style.display = "none";
            elemEarnText.style.display = "none";
         },
        function(){
            elemListen.style.display = "none";
            elemReview.style.display = "block";
            elemEarn.style.display = "none";
            elemListenText.style.display = "none";
            elemReviewText.style.display = "block";
            elemEarnText.style.display = "none";
         },
         function(){
            elemListen.style.display = "none";
            elemReview.style.display = "none";
            elemEarn.style.display = "block";
            elemListenText.style.display = "none";
            elemReviewText.style.display = "none";
            elemEarnText.style.display = "block";
         } 
      ],
      idInterval = setInterval(function(){
         for (var i = functions.length; i >= 0; i--) {
            functions[i]();
         };
      }, 5000);
}
于 2013-04-04T21:06:38.883 回答