function sjoin(o) 
{     
o.index = Math.floor(o.index*Math.random());     
return Math.round(o.year*o.month/o.date+o.index-sjoin.rand)+"-"+o.index; 

sjoin.rand = 568; //随意数(常量) 
alert(sjoin({year:2013,month:4,date:3,index:3183})); 以上代码的原要求是:年*月/日+自动编号(变量)- 随意数(常量)
其中年、月、日、自动变号(计数)都是变量,但上面的代码,经试用,发现其中的表示此四变量的四个值({year:2013,month:4,date:3,index:3183})都不自动变化。要用上面的代码计号,那需要每天都手动更改date:3的值,否则所得计号就无法表示准确的号。这样很费事,请高手想帮助办法改一下。

解决方案 »

  1.   

    用new Date()构造当前时间,然后获取年月日
    var now = new Date();
    var year = now.getFullYear(),month = now.getMonth()+1,date = now.getDate();
    sjoin({year:year,month:month,date:date,index:3183})
      

  2.   


    <script type="text/javascript">
    function getRandom(min,max){//随机数生成种子()
    return Math.round(Math.random()*(max-min)+min);
    }
    function get_k(t){//t为常数
    var obj={};
    var myDate=new Date();
    var year=myDate.getFullYear();
    var month=myDate.getMonth()+1;//1月返回0
    var date=myDate.getDate();
    var s=year*month/date+getRandom(10,100)-t;
    obj={"年":year,"月":month,"日":date,"随机数":getRandom(10,20),"常量":t,S:s};
    return obj;
    //alert(getRandom(20,30));
    }
    var t=get_k(4);
    for(i in t){
    alert("键名:"+i+"\n"+"键值:"+t[i]);
    }
    alert("最后的结果是:"+t.S);
    </script>