从数组上显示随机项按钮单击,在行中没有两个

时间:2014-05-31 20:52:27

标签: javascript arrays

我有一个按钮,显示数组中的引用和作者。每次单击按钮时,我都需要按钮来显示随机引用/作者。连续两个引号/作者都没有!

window.onload = function()
{
    //assign var to quoteText id contents
    var quoteSpan = document.getElementById("quoteText");

    //assign var to authorText id contents
    var authorSpan = document.getElementById("authorText");

    //assign var to submitButton contents   
    var submitButton = document.getElementById('submit');

    var quotes = [
        {'text': '"Whatever you are, be a good one."', 'author': '-Abraham Lincoln'}, 
        {'text': '"It has been my philosophy of life that difficulties vanish when faced boldly."', 'author': '-Isaac Asimov'}, 
        {'text': '"Enjoy life. There’s plenty of time to be dead."', 'author': '-Anonymous'}, 
        {'text': '"Every moment is a fresh beginning."', 'author': '-T.S. Eliot'}, 
        {'text': '"One day your life will flash before your eyes. Make sure it is worth watching."', 'author': '-Anonymous'}
    ];  

    var oldQuoteIndex = -1; //unfound item in array is -1

    //function determining random quote
    function nextQuote() {

        do { 
            var newQuoteIndex = Math.floor(Math.random() * quotes.length); //picks random quote index from quotes array

        }   while (newQuoteIndex == oldQuoteIndex); //while index of newly chosen quote is the same as the index of old quote

        quoteSpan.innerHTML = quotes[newQuoteIndex].text; //make HTML's quoteText random quote
        authorSpan.innerHTML = quotes[newQuoteIndex].author; //make HTML's authorText random author

        var oldQuoteIndex = newQuoteIndex; //make old index same as new index, so that next time it runs, the WHILE aspect causes DO aspect to randomize
    }

    //when button is clicked, quotation function starts
    submitButton.onclick = nextQuote;

}

2 个答案:

答案 0 :(得分:0)

您在几个地方重新声明变量而不是使用更高范围内的变量,这就是您每次都没有获得新报价的原因。

window.onload = function() {
    var quoteSpan     = document.getElementById("quoteText");
    var authorSpan    = document.getElementById("authorText");
    var submitButton  = document.getElementById('submit');
    var oldQuoteIndex = -1;
    var newQuoteIndex = -1;
    var quotes        = [
        {'text': '"Whatever you are, be a good one."', 'author': '-Abraham Lincoln'}, 
        {'text': '"It has been my philosophy of life that difficulties vanish when faced boldly."', 'author': '-Isaac Asimov'}, 
        {'text': '"Enjoy life. There’s plenty of time to be dead."', 'author': '-Anonymous'}, 
        {'text': '"Every moment is a fresh beginning."', 'author': '-T.S. Eliot'}, 
        {'text': '"One day your life will flash before your eyes. Make sure it is worth watching."', 'author': '-Anonymous'}
    ];  

    function nextQuote() {
        while (newQuoteIndex == oldQuoteIndex) {
            newQuoteIndex = Math.floor(Math.random() * quotes.length);
        }

        quoteSpan.innerHTML  = quotes[newQuoteIndex].text; //make HTML's quoteText random quote
        authorSpan.innerHTML = quotes[newQuoteIndex].author; //make HTML's authorText random author

        oldQuoteIndex = newQuoteIndex;
    }
    submitButton.onclick = nextQuote;
}

FIDDLE

答案 1 :(得分:0)

/* getNewQuote might be good in a js file to use on any page */
function getNewQuote(callback){
    var newquote;
    var quotes = getNewQuote.prototype.quotes;
    do{
        newquote = quotes[parseInt(Math.random()*quotes.length)];
    /* if quotes.length == 1 we'll loop forever */ 
    }while( quotes.length > 1 &&  !getNewQuote.prototype.isLastQuote(newquote));
    /* Good idea to check if there is a callback */
    if(callback){ 
        callback(newquote);
    }
}
getNewQuote.prototype.quotes = [
    {'text': '"Whatever you are, be a good one."', 'author': '-Abraham Lincoln'}, 
    {'text': '"It has been my philosophy of life that difficulties vanish when faced boldly."', 'author': '-Isaac Asimov'}, 
    {'text': '"Enjoy life. There’s plenty of time to be dead."', 'author': '-Anonymous'}, 
    {'text': '"Every moment is a fresh beginning."', 'author': '-T.S. Eliot'}, 
    {'text': '"One day your life will flash before your eyes. Make sure it is worth watching."', 'author': '-Anonymous'}
];  
getNewQuote.prototype.lastQuote = null;
getNewQuote.prototype.isLastQuote = function(quote){
    if(!getNewQuote.prototype.lastQuote 
        || ( getNewQuote.prototype.lastQuote.author != quote.author
            && getNewQuote.prototype.lastQuote.text != quote.text )
    ) {
        return getNewQuote.prototype.lastQuote = quote;
    }
    return null;
}
/* End of the getNewQuote */


/* This goes on the page */    
submitButton.onclick = function (){ 
    /* pass getNewQuote a callback function to update the elements with the new quote                 */
    getNewQuote(function(quote){
        quoteSpan.innerHTML = quote.text;
        authorSpan.innerHTML = quote.author;
    });
}
/* if you decide to go with jQuery you can replace the above onclick with this */
$(function(){
    $("#submitButton").click(function (){ 
        getNewQuote(function(quote){
            $("#quote").html(quote.text);
            $("#author").html(quote.author);
        });
    });
});