javascript - How can alter text in a loop in a DIV -


i have text inside div tag. possible change text content 5 different text contents in cycle never ends? sounds simple, i'm trying find easiest approach jquery or related.

many thanks, erik

really simple approach: use setinterval().

var text = ['foo', 'bar', 'baz'];     = 0,     $div = $('#mydiv');  setinterval(function () {     $div.text(text[i++ % text.length]); }, 1000); 

http://jsfiddle.net/mattball/tjv3k/


edit if want fancier effects: http://jsfiddle.net/mattball/tjv3k/1/


edit 2 re op comments (below)

  • fix #1: use distinct variable names each rotator, because variable scoping unaffected <script> tags.
  • fix #2: don't change variable names, use closures global scope isn't polluted.

other fixes needed (shown in both fiddles):

  • actually load jquery somewhere, otherwise none of code work.

    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.5.2/jquery.min.js"></script> 
  • remove trailing comma @ end of first text array literal

  • change semicolons following 2 text array declarations commas, otherwise following variables (i , $div) declared global, bad thing.

Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -