Arrays 向随机页面刷新添加cookie/无重复功能
我一直在使用这段java代码在设置的计时器上(或在页面刷新时)从49个列表中调用一个随机html页面。我想对其进行转换,以便cookie(或其他东西)保存已显示的页面,以便在刷新时,用户始终会收到一个新页面,直到列表完成,此时cookie被清空/删除 我找到了。我可以用这个吗?这也是一种变体。抱歉,我的代码很差。如有任何建议,敬请谅解:Arrays 向随机页面刷新添加cookie/无重复功能,arrays,list,cookies,random,page-refresh,Arrays,List,Cookies,Random,Page Refresh,我一直在使用这段java代码在设置的计时器上(或在页面刷新时)从49个列表中调用一个随机html页面。我想对其进行转换,以便cookie(或其他东西)保存已显示的页面,以便在刷新时,用户始终会收到一个新页面,直到列表完成,此时cookie被清空/删除 我找到了。我可以用这个吗?这也是一种变体。抱歉,我的代码很差。如有任何建议,敬请谅解: <script type="text/javascript"> var sites = [ "nam
<script type="text/javascript">
var sites = [
"name1.html",
"name2.html",
"name3.html",
"name...etc.html",
"name49.html",
]
$(document).ready(function() {
newPage();
});
function newPage()
{
setTimeout(newPage, 60000);
var min = 0;
var max = 48;
var num = Math.floor(Math.random() * (max - min + 1)) + min;
$('#target').attr('src',sites[num]);
}
</script>
变量站点=[
“name1.html”,
“name2.html”,
“name3.html”,
“name…etc.html”,
“name49.html”,
]
$(文档).ready(函数(){
newPage();
});
函数newPage()
{
设置超时(新页,60000);
var min=0;
var max=48;
var num=Math.floor(Math.random()*(max-min+1))+min;
$('#target').attr('src',sites[num]);
}
看起来您正在使用jQuery,因此我建议您使用jQuery插件来更灵活地管理cookie
将此URL的内容复制并粘贴到服务器上的新js文件,并将其包含在jquery文件之后:
或者,您也可以使用您提到的另一个答案中的get/set cookie函数
要记住的主要一点是cookie存储为字符串,因此您将join()
ing和split()
ing数组
另一件需要注意的事情是,因为我们想要从我们没有访问过的项目中随机获得一个项目,所以跟踪这些项目比跟踪我们访问过的项目更有效
这意味着我们总是从剩下的东西中随机选择一个项目,而不是在所有东西上循环,每次都检查我们是否已经在那里,因为这样会变得非常低效
var sites = [ 'name1.html', 'name2.html', 'name3.html', ...etc... ],
unvisited = sites.slice(0); // .slice(0) clones the array
// if the cookie has a value then make it into the unvisited array
if ( $.cookie( 'unvisited' ) )
unvisited = $.cookie( 'unvisited' ).split( ',' );
$(document).ready(function() {
newPage();
});
function newPage() {
setTimeout( newPage, 60000 );
// check if the unvisited array needs resetting
if ( unvisited.length == 0 ) {
unvisited = sites.slice(0);
$.removeCookie( 'unvisited' );
}
// get a new index from the unvisited array
var num = Math.floor( Math.random() * unvisited.length );
// remove the item from the array and save the cookie
var site = unvisited.splice( num, 1 )[ 0 ];
// save the unvisited array minus the site we just spliced out
$.cookie( 'unvisited', unvisited.join( ',' ) );
$( '#target' ).attr( 'src', site );
}
var numSites=49;
var seen=新阵列(numSites);
$(文档).ready(函数(){
var cookie=unescape(getCookie(“seen”);
seen=cookie?cookie.split(','):seen;
setTimeout(gotoNext,60000);
});
函数gotoNext(){
var num=getRandom();
var计数=0;
while(见[num]==1){
num++;
如果(num>=numSites){
num=0;
计数++;
如果(计数>1){
resetSeen();
num=getRandom();
}
}
}
seen[num]=1;
setCookie(“seen”,escape(seen.join(',')),365);
window.location=“name”+num+“.html”;
}
函数getCookie(cname){
变量名称=cname+“=”;
var ca=document.cookie.split(“;”);
for(var i=0;它看起来很完美。谢谢。只需检查一下,但此部分会在访问所有站点后删除cookie?:if(unvisited.length==0){unvisited=sites.slice(0);$.removeCookie('unvisited');}抱歉,我想我在twitter上回答了这个问题-是的,它会将未访问的数组重置为包含所有站点
var numSites = 49;
var seen = new Array(numSites);
$(document).ready(function() {
var cookie = unescape(getCookie("seen"));
seen = cookie ? cookie.split(',') : seen;
setTimeout(gotoNext, 60000);
});
function gotoNext() {
var num = getRandom();
var count = 0;
while (seen[num] == 1) {
num++;
if (num >= numSites) {
num = 0;
count++;
if (count > 1) {
resetSeen();
num = getRandom();
}
}
}
seen[num] = 1;
setCookie("seen", escape(seen.join(',')), 365);
window.location = "name" + num + ".html";
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for(var i=0; i<ca.length; i++) {
var c = ca[i].trim();
if (c.indexOf(name)==0) return c.substring(name.length, c.length);
}
return "";
}
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays*24*60*60*1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
function resetSeen() {
for (var i=0; i<numSites; i++) {
seen[i] = "";
}
}
function getRandom() {
return Math.ceil(Math.random() * numSites);
}