每周显示随机数

时间:2016-09-15 21:59:43

标签: javascript jquery

我想显示1,000到1,999之间的随机数,并显示一周的数字(例如星期一)。然后下周,为该周生成一个新号码。

下面的代码生成一个随机数,但没有逗号和存储空间。有什么帮助吗?

var minNumber = 1001;
var maxNumber = 1997

weeklyNumber(minNumber, maxNumber);

function weeklyNumber(min,max)
{
    alert(Math.floor(Math.random()*(max-min+1)+min));
}
<span class="weekly-count"></span>

1 个答案:

答案 0 :(得分:0)

试试这个:

$(document).ready(function(){

     var minNumber = 1001;
     var maxNumber = 1997;


     if (!localStorage.getItem("ran") && !localStorage.getItem("myTime") ) {

         var d = Date.now();
         var RandomNum = Math.floor((Math.random()*(maxNumber-minNumber+1))+minNumber);

         localStorage.setItem("ran",RandomNum);
         localStorage.setItem("myTime",d);

     }


     else {

         var newTime = Date.now();
         var oldTime = localStorage.getItem("myTime");

         if(newTime - oldTime > 7*24*60*60*1000) { 

             RandomNum = Math.floor((Math.random()*(maxNumber-minNumber+1))+minNumber);
             localStorage.setItem("ran",RandomNum);
             localStorage.setItem("myTime",newTime);

         }

     }

     $(".weekly-count").text(localStorage.getItem("ran"))

 })

这是Demo