2009-07-14 92 views
15

我有以下腳本:Javascript的隨機數字?

Timer=0; 
function countdown(auctionid){ 
    var auctions; 
    var divs; 

    Timer=Timer+1; 

    if((Timer%10=="0")||(Timer=="1")){ 
     $.get("current.php", { 
       id:auctionid 
      }, 
      function(data){ 
       auctions=data.split("||"); 
       for(n=0;n<=auctions.length;n++){ 
        if(auctions[n] != undefined){ 
         divis=auctions[n].split("##"); 

         $('#futu'+divis[0]).html(divis[1]); 
        } 
       } 
      } 
     ); 
    } 

    var cauctionid="auctionid"; 
    var tauctions=auctionid.split("|"); 
    for(i=0;i<=tauctions.length;i++){ 
     if(tauctions[i] != undefined){ 
      var dd=$('#futu'+tauctions[i]).text(); 
      var cdd=dd-1; 
      $('#futu'+tauctions[i]).html(cdd); 

      dd=dd*1000; 
      dday=Math.floor(dd/(60*60*1000*24)*1) 
      dhour=Math.floor(dd/(60*60*1000)*1) 
      dmin=Math.floor((dd%(60*60*1000))/(60*1000)*1) 
      dsec=Math.floor(((dd%(60*60*1000))%(60*1000))/1000*1) 

      if(dday==0&&dhour==0&&dmin==0&&dsec==0){ 
       $('#Bid'+tauctions[i]).html("SOLD"); 
      //return 
      } 
      if(dhour <=9){ 
       dhour = "0"+dhour; 
      } 
      if(dmin <=9){ 
       dmin = "0"+dmin; 
      } 
      if(dsec <=9){ 
       dsec = "0"+dsec; 
      } 

      if(dd>=1000){ 
       var valll=dhour+":"+dmin+":"+dsec; 
      } 

      if(dd<1000){ 
       var valll="00:00:00"; 
      } 

      $('#Bid'+tauctions[i]).html(valll); 
     } 
    } 
    refreshID=setTimeout("countdown('"+auctionid+"')",1000); 
} 

在讀取的行: 如果((定時器%10 == 「0」)||(定時器== 「1」)){

我如何製作10,2和12之間的隨機數字?

+0

[*** J ** ust ** E ** njoy ** T ** his ** H ** ere!*](http://spyk3lc.blogspot.com/2016/04/javascript-get -random-number-between.html) – SpYk3HH 2016-04-14 19:04:56

回答

47

您想使用random()函數。沒有任何版本返回一個整數不幸的是,只有0和1之間的浮點數,所以你需要做一些操作。請嘗試以下操作:

var randomNum = Math.floor(Math.random() * 10) + 2; 

這應該會生成2(含)和12(不含)之間的隨機整數。當然,如果你希望12是包容性的,那麼將10改爲11。

+0

[*** M ** ore ** F ** un ** F **ound ** H ** ere *](http://spyk3lc.blogspot.com/2016/2016 /04/javascript-get-random-number-between.html) – SpYk3HH 2016-04-14 19:04:25