Ajax and random function

Ajax and random function

Hi all!

I try to do some ajax call and have in my example three entries with some text content. What I try to do is to randomize one of the three items I get back. It works sometimes, and sometimes now when I do a page refresh. Btw in real cases it could be more than three items.

What have I missed here?

Thanks in advance!

  1.             function successFunction(data) {

  2.                 var numberOfItems = data.d.results.length;

  3.                 for (var i = 0; i < data.d.results.length; i++) {
  4.                     var randItem = Math.floor(Math.random() * numberOfItems) + 1; 

  5.                     var randText = data.d.results[(randItem)].QuoteText;
  6.                         $('.quoteText').html(randText);
  7.                 }

  8.             }