简体   繁体   English

setInterval()和.delay()

[英]setInterval() and .delay()

 // hide word-rotate spans $('.word-rotate').hide(); // Set variables var words = [], index = 0, $span = $('.text-rotate'); // Get words within 'word-rotate' spans and push in array $('.word-rotate').each(function() { words.push($(this).text()); }); // Rotate function function rotateFunction() { $span.text(words[index]).fadeIn(0); if ( index == words.length -1 ) { $span.css('color', '#F42156'); $span.delay(50000).fadeOut(0); index = 0; } else { $span.css('color', '#00FFEE'); $span.delay(500).fadeOut(0); index++; } } setInterval(rotateFunction, 500); 
 html { background: #313449; color: #ffffff; display: flex; height: 100%; justify-content: center; align-items: center; font-family: 'Open Sans', sans-serif; text-align: center; } .text-rotate { color: #00FFEE; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> <div> <h1>Make it more <br /> <span class="text-rotate"></span> <span class="word-rotate">attractive</span> <span class="word-rotate">fun</span> <span class="word-rotate">colorful</span> <span class="word-rotate">fancy</span> <span class="word-rotate">intelligent.</span> </h1> </div> 

I am trying to figure out how to create a word-changing system with a longer time fixed on the last word of my list. 我试图弄清楚如何创建一个更长的时间固定在列表的最后一个单词上的单词转换系统。

It is working for the rotating part but I cannot figure out how to make a pause on the last word. 它适用于旋转部分,但我不知道如何在最后一个单词上稍作停顿。 I changed the .delay to 5000 but it doesn't work as expected. 我将.delay更改为5000,但无法正常工作。

I want to have each word appearing 500ms and the last one 5s. 我希望每个单词出现500毫秒,最后一个5秒钟。

Why is this .delay() not working ? 为什么这个.delay()不起作用?

here is the codepen: http://codepen.io/mouuuton/pen/ZBzqGL/ 这是codepen: http ://codepen.io/mouuuton/pen/ZBzqGL/

 // hide word-rotate spans $('.word-rotate').hide(); // Set variables var words = [], index = 0, $span = $('.text-rotate'); // Get words within 'word-rotate' spans and push in array $('.word-rotate').each(function() { words.push($(this).text()); }); // Rotate function function rotateFunction() { $span.text(words[index]).fadeIn(0); if ( index == words.length -1 ) { $span.css('color', '#F42156'); $span.delay(50000).fadeOut(0); index = 0; setTimeout(rotateFunction, 5000); } else { $span.css('color', '#00FFEE'); $span.delay(500).fadeOut(0); index++; setTimeout(rotateFunction, 500); } } setTimeout(rotateFunction, 500); 
 html { background: #313449; color: #ffffff; display: flex; height: 100%; justify-content: center; align-items: center; font-family: 'Open Sans', sans-serif; text-align: center; } .text-rotate { color: #00FFEE; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script> <div> <h1>Make it more <br /> <span class="text-rotate"></span> <span class="word-rotate">attractive</span> <span class="word-rotate">fun</span> <span class="word-rotate">colorful</span> <span class="word-rotate">fancy</span> <span class="word-rotate">intelligent.</span> </h1> </div> 

I wouldn't use delay and setInterval and would do it like this: 我不会使用delaysetInterval ,而是这样做的:

function rotateFunction() {
  $span.text(words[index]).fadeIn(0);
  if ( index == words.length -1 ) {
    setTimeout(function() {
      $span.css('color', '#F42156');
      $span.delay(50000).fadeOut(0);
      index = 0;
      rotateFunction();
    }, 5000);
  } else {
    setTimeout(function() {
      $span.css('color', '#00FFEE');
      $span.delay(500).fadeOut(0);
      index++;
      rotateFunction();
    }, 500);
  }
}

rotateFunction();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM