js雙色球隨機選號程式碼例項

admin發表於2017-04-16

分享一段程式碼例項,它實現了雙色球隨機選號功能。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style>
li {
  width: 40px;
  height: 40px;
  background: red;
  color: #fff;
  margin: 5px;
  float: left;
  list-style: none;
  border-radius: 50%;
  text-align: center;
  line-height: 40px;
  font-size: 18px;
  font-weight: bold;
}
</style>
<script>
function toDouble(n) {
  return n < 10 ? '0' + n : '' + n
};
 
function ran(n, m) {
  return parseInt(Math.random() * (m - n) + n)
}
 
function findInArr(n, arr) {
  for (var i = 0; i < arr.length; i++) {
    if (n == arr[i]) {
      return true;
    }
  }
  return false;
};
 
function toSort() {
  sort(function(a, b) {
    if (a < b) {
      return b - a
    }
  })
}
//alert(findInArr(5,[123456789]))
 
//document.write(arr);
window.onload = function() {
  var oInput = document.getElementById('input');
  var timer = null;
 
  function scroll() {
    var aLi = document.getElementsByTagName('li');
    aLi[aLi.length - 1].style.background = 'blue';
    var arr = [];
    while (arr.length < 6) {
      var n = toDouble(ran(1, 34));
      if (!findInArr(n, arr)) {
        arr.push(n);
        var arr2 = arr.sort(function(a, b) {
          if (a > b) {
            return a - b
          }
        });
      }
    }
    for (var index = 0; index < aLi.length; index++) {
      aLi[index].innerHTML = arr2[index];
    }
    aLi[aLi.length - 1].innerHTML = toDouble(ran(1, 17));
  }
  scroll();
  oInput.onclick = function() {
    clearInterval(timer);
    timer = setInterval(scroll, 10);
    setTimeout(function() {
      clearInterval(timer)
    }, 600)
  }
}
</script>
</head>
<body>
<input type="button" value="隨機一注" id="input">
<ul>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
  <li></li>
</ul>
</body>
</html>

相關文章