0

I need to fade items from an array in and out continuously and I'm currently doing it as such:

var rotateHint = function() {
var hints = ['fe','fi','fo','fum'];
var hint;
var p = (function() {
    if (!hint || !hint.length) hint = hints.slice();
         return hint.splice(Math.random() * hint.length | 0, 1);
}())
$('#hint').text(p) //THE ERROR IS HERE...
          .fadeIn()
          .delay(1000)
          .fadeOut(200,function(){ 
            rotateHint();
            })
};

However it seems that .text(p) isn't working however if I look at p with an alert(p) the output is sound.

6
  • What element is #hint? Have you tried using html() instead of text() instead? Commented Aug 20, 2012 at 14:34
  • 1
    Can you set up a fiddle on jsfiddle.net showing the issue? Commented Aug 20, 2012 at 14:35
  • #hint is a simple <div></div> and yes I've also tried html().. Commented Aug 20, 2012 at 14:35
  • Works fine for me. jsfiddle.net/fpHKq Commented Aug 20, 2012 at 14:38
  • yeh seems my jQuery is below 1.7 thanks guys!! Commented Aug 20, 2012 at 14:52

2 Answers 2

2

It seems to be working only from jQuery 1.7 Previous versions throw an error

But it's kind of weird to do a text() on an array no?

Sign up to request clarification or add additional context in comments.

Comments

-1

Check out this Fiddle. Seems it is already working.

For some added value I optimized your JavaScript code.

HTML

<div id="hint"></div>​

JavaScript

(function() {
    var hints = ['fe', 'fi', 'fo', 'fum']
      , p;

    (function rotateHint() {
        p = hints[Math.round(Math.random() * hints.length)];

        $('#hint').text(p).fadeIn().delay(1000).fadeOut(200, rotateHint);
    }());

}());​

Works on Safari 6, FireFox 14 and Chrome 21 Beta

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.