Search code examples
javascriptjqueryhtmlswaptimed

Timed text swap?


is it possible to swap a paragraph of text with javascript/jQuery? I want a delay timer of about 5 seconds, and then the text should swap to something else, like a image slide. Would be awesome with a fade or an effect, but whatever works. Can you please point me in the right direction or help me out?


Solution

  • Here is how to loop without setTimeout or setInterval

    DEMO HERE

    <div id="textMessage"></div>
    <div class="textContent" style="display:none">Lorem ipsum dolor sit amet</div>
    <div class="textContent" style="display:none">In sit amet diam et arcu aliquam tincidunt. </div>
    
    function slide() {
      if (cnt>=texts.length) cnt=0;
      $('#textMessage').html(texts[cnt++]);
      $('#textMessage')
        .fadeIn('slow').animate({opacity: 1.0}, 3000).fadeOut('slow',
         function() {
           return slide()
         }
      );      
    }      
    $(document).ready(function() {
      // save the texts in an array for re-use
      $(".textContent").each(function() {
        texts[cnt++]=$(this).text();
      });
      slide()  
    });