JS选择页面刷新随机引号

时间:2018-03-24 18:36:10

标签: javascript html

我正在制作我的网站,我想要它,以便每次用户刷新页面时,会弹出一个新的随机笑话而不是之前选择的笑话。这是我现在的剧本:

function ShowJoke(){
  var Joke=["Jokes", "go", "here"];
  var Pick = Math.floor(Math.random() * (Joke.length));
  document.write(Joke[Pick]);
}

3 个答案:

答案 0 :(得分:2)

我只能回复您提供的信息,所以如果出现问题,请告诉我,以便我可以解决。

因此,根据我对此问题的看法,您希望在页面加载时加载新的笑话。 “每次页面刷新一个新的随机引用而不是最后一个”

注意:由于“allow-same-origin”标志,下面的代码段可能无效,除非您使用的是XAMPP,否则很可能无法在本地托管。

throwdown是检查cookie是否存在,如果存在,继续生成,直到生成一个不等于cookie的新笑话,设置新cookie,并显示笑话。

// Array of Jokes
var Jokes = [
"I just got fired from my job at the keyboard factory. They told me I wasn't putting in enough shifts.",
"We'll we'll we'll...if it isn't autocorrect.",
"Q. Which type of vegetable tries to be cool, but is only partly successful at it?\n\nA. The radish.",
"The world tongue-twister champion just got arrested. I hear they're gonna give him a really tough sentence."
];

// function to check cookie (true if exists, false if not)
function checkCookie(){
    var joke = getCookie();
    if (joke != "") {
      return true;
    } else {
      return false;
    }
}
// set the cookie so can be referenced later
function setCookie(cvalue){
    var cname = "joke";
    var d = new Date();
    d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
    var expires = "expires="+d.toUTCString();
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
// actually acquire the cookie and read it
function getCookie() {
    var cname = "joke";
    var name = cname + "=";
    var decodedCookie = decodeURIComponent(document.cookie);
    var ca = decodedCookie.split(';');
    for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}

function ShowJoke(){
  let randomNum = ~~(Math.random() * Jokes.length); // pick a random number from 0 to length of jokes
  if(checkCookie()){ // check if cookie exists
    while(Jokes[randomNum] != getCookie()) randomNum = ~~(Math.random() * Jokes.length);
    } // while cookie's joke != generated joke
  document.getElementById('Joke').textContent = Jokes[randomNum]; // set content
  setCookie(Jokes[randomNum]); // set cookie
}
window.onload = ShowJoke(); // run on window load
<p id="Joke"></p>

答案 1 :(得分:1)

您的代码看起来很好。但是,您有语法错误。此外,你没有打电话给结局

function ShowJoke()
  {
    var Joke = [
    	'Joke1',
    	'Joke2',
    	'Joke3',
    	'Joke4',
    	'Joke5',
    	'Joke6'
    ];
    var Pick = Math.floor(Math.random() * (Joke.length));
    document.write(Joke[Pick]);
  }
  
  document.addEventListener("load", ShowJoke());

答案 2 :(得分:0)

你只需要解决拼写错误

function ShowJoke()
  {
    var Joke=[1, 2, 3, 4, 5, 6, 7];
    var Pick = Math.floor(Math.random() * (Joke.length));
    document.write(Joke[Pick]);
  }

并实际调用函数

ShowJoke();

编辑:

事实证明,op不想重复相同的笑话,因此我们可以这样做:

function ShowJoke()
  {
    var RawJoke=[1, 2, 3, 4, 5, 6, 7];
    var ToldJokes = localStorage.getItem("jokes");
    if (ToldJokes) {
        ToldJokes = JSON.parse(ToldJokes);
    } else {
        ToldJokes = [];
    }
    var Joke = [];
    for (var i = 0; i < RawJoke.length; i++)
        if (ToldJokes.indexOf(RawJoke[i]) === -1) 
            Joke.push(RawJoke[i]);
    if (Joke.length === 0) {
        ToldJokes = [];
        Joke = RawJoke;
    }
    var Pick = Math.floor(Math.random() * (Joke.length));
    ToldJokes.push(Joke[Pick]);
    localStorage.setItem("jokes", JSON.stringify(ToldJokes));
    document.write(Joke[Pick]);
  }