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
textarray literal- change semicolons following 2
textarray declarations commas, otherwise following variables (i,$div) declared global, bad thing.
Comments
Post a Comment