Javascript 如何使重定向不随机并遵循顺序?

Javascript 如何使重定向不随机并遵循顺序?,javascript,redirect,Javascript,Redirect,我有这样一段代码,每次同一个用户重新加载页面时,它都会将页面重定向到不同的URL,问题是有时它会重定向到列表中的同一URL,而我需要的是遵循一个顺序。 例如:url1、url2、url3 var urlist=[“url1.html”、“url2.html”、“url3.html”]; var重设=0000; setTimeout(函数(){ window.location=urlist[Math.floor(Math.random()*urlist.length)]; },重新铺设); 可能类

我有这样一段代码,每次同一个用户重新加载页面时,它都会将页面重定向到不同的URL,问题是有时它会重定向到列表中的同一URL,而我需要的是遵循一个顺序。 例如:url1、url2、url3

var urlist=[“url1.html”、“url2.html”、“url3.html”];
var重设=0000;
setTimeout(函数(){
window.location=urlist[Math.floor(Math.random()*urlist.length)];
},重新铺设);
可能类似于:

var URLlist = ["url1.html","url2.html","url3.html"];
//URLs a las que el usuario ha sido redireccionado.
var urlsRedireccionado = JSON.parse(window.localStorage.getItem("urls_redireccionado")) || [];

var reDelay = 0000;

setTimeout(function(){
    //La siguiente URL en el orden en que han sido agregadas en la variable URLlist
    var next = URLlist.find(u => !urlsRedireccionado.some(ur => ur == u));
    //Si faltan URL por redireccionar
    if(next) {
        urlsRedireccionado.push(next);
        window.localStorage.setItem("urls_redireccionado", JSON.stringify(urlsRedireccionado));
        window.location = next;
    } else {
        //Para que la proxima vez que recargue sea redirigido a todas las URL anteriores
        window.localStorage.removeItem("urls_redireccionado");

        //Redireccionado a todas partes
    }

}, reDelay);

您可以使用
window.location.href
检查页面的当前URL,并从URL列表中筛选它。然后,只有不是当前页面的选项可以选择为新URL

/**
*示例当前URL。
*其值应为当前页面的URL。
*比如window.location.href
*/
var currentURL='url2.html';
//从列表中筛选当前页面。
var urlist=[“url1.html”、“url2.html”、“url3.html”].filter(url=>url!==currentURL);
var重设=1000;
setTimeout(函数(){
var newURL=urlist[Math.floor(Math.random()*urlist.length)];
//window.location=newURL;
console.log(newURL);

},重新铺设)尝试添加一个计数器,该计数器将帮助您跟踪URL位置,以及一个条件,该条件将帮助您在URL不足时将计数器重置为数组中的第一个位置

var urlist=['url1.html','url2.html','url3.html'];
var计数器=0;
风险价值重设=2000;
setTimeout(函数(){
window.location=urlist[counter];
计数器++;
URLlist[计数器]==未定义?计数器=0:计数器;

},重新铺设)您需要跟踪用户访问过的内容。在应用程序中使用localStorage、cookie或状态容器有助于实现这一点。不要使用random来选择一个。创建URL后,将URL列表洗牌。