js双色球随机选号代码实例

快乐打工仔 分类:实例代码

分享一段代码实例,它实现了双色球随机选号功能。

代码实例如下:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.pipipi.net/" />
<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>

js双色球随机选号代码实例,这样的场景在实际项目中还是用的比较多的,关于js双色球随机选号代码实例就介绍到这了。

js双色球随机选号代码实例属于前端实例代码,有关更多实例代码大家可以查看

回复

我来回复
  • 暂无回复内容