在将其加载一段时间后,淡入和淡出速度比预期的快了3倍(一开始它可以正常工作)。任何帮助和解释我做错了什么?谢谢。

<html>
<head>
<script src='http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js' type='text/javascript'></script>
<script type='text/javascript'>
$(document).ready(function(){
    var x =-1
    function setWord(){
        function come(){
            $(".fde").fadeIn(200);
        }
        come();
        function fade(){
            $(".fde").fadeOut(200);
        }
        setTimeout(fade, 2800);
        var phrases =new Array("War is peace","Freedom is slavery","Ignorance is strength");
        if (x == phrases.length-1){x = -1}
        x += 1;
        $(".test").text(phrases[x]);
    }
    setTimeout(setWord,0);
    setInterval(setWord, 3000);
});
</script>
</head>
<body>
<p class="fde"><span class='test'></span></p>
</body>
</html>

最佳答案

jsBin DEMO

实际上,您不需要任何setIntervalsetTimeout,只需使用.animate()回调即可再次运行您的函数:

$(function(){ // DOM ready

  var x = 0,
      $test = $('.test'),
      phrases = ["War is peace","Freedom is slavery","Ignorance is strength"],
      n = phrases.length;

  function loopWords(){
     $test.text(phrases[x++%n]).parent().fadeTo(500,1).delay(2000).fadeTo(500, 0, loopWords);
  }
  loopWords(); // Start

});

关于javascript - 一段时间后,淡入和淡出变得非常快[javascript],我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22543404/

10-11 20:04