随意引用没有背靠背

时间:2016-02-01 17:31:02

标签: javascript jquery arrays random

我试图从数组中提取随机引用。我需要显示一个初始报价,然后得到一个新报价,没有相同的两个报价背靠背。这就是我得到的。

$(document).ready(function() {

    var quoter = [{
    quote: "I drink to make other people more interesting.",
    author: "Ernest Hemingway"
}, {
    quote: "Alcohol may be man's worst enemy, but the bible says love your enemy.",
    author: "Frank Sinatra"
}, {
    quote: "Reality is an illusion created by a lack of alcohol.",
    author: "N.F. Simpson"
},{
    quote: "Time is never wasted when you’re wasted all the time.",
    author: "Catherine Zandonella"
},{
    quote: "I feel bad for people who don’t drink. When they wake up in the morning, that’s as good as they’re going to feel all day.",
    author: "Frank Sinatra"    
}];

    var randomQuote = Math.floor(Math.random() * quoter.length);
//$(function () {
    //Set Original Quote
    $('#quoteText').text(quoter[randomQuote].quote);
    $('#authorText').text(quoter[randomQuote].author);      
//});       

  $('#btnNew').click(function(evt) {
    //prevent browser's default action
    evt.preventDefault();
    //getting a new random number to attach to a quote and setting a limit
    var sourceLength = quoter.length;
    var randomNumber = Math.floor(Math.random() * sourceLength);


    //set a new quote
    //while ( randomNumber <= sourceLength ) {
        while (randomNumber === randomNumber){
      var newQuoteText = quoter[randomNumber].quote;
      var newQuoteGenius = quoter[randomNumber].author;

      var timeAnimation = 500;
      var quoteContainer = $('#quoteContainer');
      //fade out animation with callback
      quoteContainer.fadeOut(timeAnimation, function() {

        //set text values
        $('#quoteText').text(newQuoteText);
        $('#authorText').text(newQuoteGenius);

        //console.log(quoteText,authorText);

        //fadein animation.
        quoteContainer.fadeIn(timeAnimation);
      });

      break;

    }; //end while loop

  }); //end btnNew function

}); //end document ready

我需要使用while循环来完成此操作。我无法弄清楚如何存储随机引用(数组值),然后将其与新的随机引用进行比较,以获得不同的随机引用(如果它相同)。

HTML在这里:

<div id="quoteContainer">
   <p><span id="quoteText"></span><Br/></p>
   &mdash;<span id="authorText"> </span>
</div>

2 个答案:

答案 0 :(得分:2)

你可以让一个简单的变量存储前一个值,然后检查随机数是否与上次相同。

$(document).ready(function(){
    //....your current above code

    var lastQuote = randomQuote;

    $(button).click(function(){
        var thisQuote = Math.floor(Math.random() * sourceLength);

        //This will only be entered if the two are equal
        //The while loop ensures that if you get a new random number
        //it won't be the same
        while(thisQuote == lastQuote){
            thisQuote = Math.floor(Math.random() * sourceLength);
        }

        //If you make it here a unique number has been found
        lastQuote = thisQuote;    
    }); 
});

答案 1 :(得分:1)

您的方法可能会导致无限循环。不会发生,因为随机数发生器并不完美,但可以更轻松地完成:

shuffel the array并线性下降(或根据需要构建一个花哨的generator)或只删除已经显示的每个引用。