一段时间后,淡入淡出变得非常快

Fading in and out becomes very quick after some time [javascript]

本文关键字:非常 淡出 淡入 一段时间      更新时间:2023-09-26

在我加载了一段时间后,淡入和淡出变得比预期快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
});

尝试将setInterval值增加到5000/10000。