请问 html网页 页面 如何生成递减随机数

2025年05月07日 06:57
有1个网友回答
网友(1):





Document

window.onload = function(){
//获取之前的随机数
var numCookie = window.name;
if(numCookie)
{
num = Math.floor(Math.random() * parseInt(numCookie));
}else{
num = Math.floor(Math.random() * 100);
}
window.name = num;
document.write(num);
}

function setCookie(c_name,value,expiredays)
{
try{
var exdate=new Date()
exdate.setDate(exdate.getDate()+expiredays)
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString())
}catch(err){
alert(err);
}
}

function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=")
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1 
    c_end=document.cookie.indexOf(";",c_start)
    if (c_end==-1) c_end=document.cookie.length
    return unescape(document.cookie.substring(c_start,c_end))
    } 
  }
return ""
}





思路是这样,你需要把上次生成的随机数存下来,存下来的方法呢有几种,这里说两种,也就是代码里写到的,一种利用window.name属性,一种利用cookie,代码贴了,仅供参考