Вопрос

After I leave this loaded for some time, the fading in and out becomes about 3 times quicker than it is intended for (at the beginning it works correctly). Any help and maybe explanation what have I done wrong? Thank you.

<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

Actually you don't need any setInterval or setTimeout, you can simply use the .animate() callback to run again your function:

$(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

});

Другие советы

Try increasing setInterval value to 5000 / 10000.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top