随机更改网页而不会两次获得同一页面

changing webpage randomly without getting the same page twice

本文关键字:两次 一页 网页 随机      更新时间:2023-09-26

我正试图用math.random链接35个网页。然而,我不希望它多次访问每个网站。所以我有这个:

function myFunction() {
var pages = ['sang1.html', 'sang2.html', 'sang3.html', 'sang4.html', 'sang5.html', 'sang6.html', 'sang7.html', 'sang8.html', 'sang9.html', 'sang10.html', 'sang11.html', 'sang12.html', 'sang13.html', 'sang14.html', 'sang15.html', 'sang17.html', 'sang18.html', 'sang19.html', 'sang20.html', 'sang21.html'];
var page, visitedPages = JSON.parse(localStorage.getItem("visitedPages"));
if (visitedPages === null) {
    visitedPages = [];
}
if (pages.length !== visitedPages.length) {
    for (var i = 0; i < pages.length; i++) {
        page = pages[Math.floor((Math.random() * pages.length) + 1)];
        if (visitedPages.indexOf(page) === -1) { //unvisited yet
            localStorage.setItem("visitedPages", JSON.stringify(visitedPages.push(page)));
            window.location.href = page;
            break;
        }
    }
} else {window.location.href = score.html //All pages visited at once
}
}

随机页面:

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Gæt en sang</title>
<link rel="stylesheet" type="text/css" href="main.css">
<script src="Test.js"></script>
</head>
<body>
    <audio class="hidden" controls autoplay>
  <source src="horse.ogg" type="audio/ogg">
  <source src="Original GhostBusters Theme Song.mp3" type="audio/mpeg">
    </audio>
        <div id="header">
            <h2> Gæt sangen og hejs flagstangen!</h2>
        </div>
        <div id="left">
        <ul> Hvilken sang er dette?
            </br>
            <button type="button" onclick="myFunction()">
            <li> Ghost busters</li>
            </button>
            </br>
            <button type="button" onclick="myFunction()">
            <li> Poltergeist</li>
            </button>
            </br>
            <button type="button" onclick="myFunction()">
            <li> Something strange<li>
            </button>
            </br>
            <button type="button" onclick="myFunction()">
            <li> Who are you gonna call<li>
            </button>
        </ul>
        </div>
   </div>
    <p id="Test"></p>
</body>
</html>

所以我有一个按钮,你按下它,它就会启动我的功能。这适用于我的所有网站。然而,当我按下按钮时,它总是转到第2页。仅从第1页开始。有什么建议吗?

与其有一个访问过的页面列表,不如有一个所有页面的列表,每次点击都可以从中随机拼接一个。没有最后一页的例子:

var pages = JSON.parse(localStorage.getItem("pages"));
if (pages === null) {
    pages = ['sang1.html', 'sang2.html', 'sang3.html', 'sang4.html', 'sang5.html', 'sang6.html', 'sang7.html', 'sang8.html', 'sang9.html', 'sang10.html', 'sang11.html', 'sang12.html', 'sang13.html', 'sang14.html', 'sang15.html', 'sang17.html', 'sang18.html', 'sang19.html', 'sang20.html', 'sang21.html'];   
}
function onClick () {   
    var randomIdx = Math.floor(Math.random() * pages.length),
        page = pages[randomIdx];
    pages.splice(randomIdx, 0);
    localStorage.setItem("pages", JSON.stringify(pages));
    window.location.href = page;
}