首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >setInterval()和.delay()

setInterval()和.delay()
EN

Stack Overflow用户
提问于 2016-11-03 09:24:10
回答 2查看 255关注 0票数 0

代码语言:javascript
运行
复制
// 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); 
代码语言:javascript
运行
复制
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;
}
代码语言:javascript
运行
复制
<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>

我正在试图找出如何创建一个更长的时间,在我的名单上的最后一个字改变的系统。

它是为旋转的部分工作,但我不知道如何暂停最后一个字。我将.delay更改为5000,但不像预期的那样工作。

我想让每个词出现在500毫秒和最后一个5s。

为什么这个.delay()不能工作?

以下是代码:http://codepen.io/mouuuton/pen/ZBzqGL/

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-11-03 09:35:25

代码语言:javascript
运行
复制
// 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);
代码语言:javascript
运行
复制
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;
}
代码语言:javascript
运行
复制
<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>

票数 0
EN

Stack Overflow用户

发布于 2016-11-03 09:38:18

我不会使用delaysetInterval,而是这样做的:

代码语言:javascript
运行
复制
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();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/40397822

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档