jQuery每两秒用一个数组中的单词替换文本 [英] Jquery replace text every 2 seconds with words from an array

查看:96
本文介绍了jQuery每两秒用一个数组中的单词替换文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 我想用一个数组中的单词替换单词中的文本, $( '#词语')延迟(1000).fadeOut(1000); 
$(this).delay(3000).text('word2')。fadeIn(1000);
$(this).delay(5000).text('word3')。fadeIn(1000);
$(this).delay(7000).text('word4')。fadeIn(1000);

这是我得到的,但显然它在7秒后停止工作..我如何重复这个?甚至可以用一个数组来保存这些单词..谢谢!

解决方案

您可以使用 setInterval )来做到这一点:



$(function(){count = 0; wordsArray = [Beta, Gamma,Delta,Alpha]; setInterval()函数(){count ++; $(#word)。 );});},2000);});

 < script src =https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js>< / script>< div id =word> Alpha< / div>  

I want to replace a text in a span with the id "words" with words from an array every 2 seconds

$('#words').delay(1000).fadeOut(1000);
    $(this).delay(3000).text('word2').fadeIn(1000);
$(this).delay(5000).text('word3').fadeIn(1000);
$(this).delay(7000).text('word4').fadeIn(1000);

This is what I've got but obviously it stops working after 7 seconds.. how can I repeat this? Or even use an array to hold the words.. Thank you!

解决方案

You can use setInterval() to do this:

$(function () {
  count = 0;
  wordsArray = ["Beta", "Gamma", "Delta", "Alpha"];
  setInterval(function () {
    count++;
    $("#word").fadeOut(400, function () {
      $(this).text(wordsArray[count % wordsArray.length]).fadeIn(400);
    });
  }, 2000);
});

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="word">Alpha</div>

这篇关于jQuery每两秒用一个数组中的单词替换文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆