javascript-抽奖

var data = ['Phone5','Ipad','三星笔记本','佳能相机','惠普打印机','谢谢参与','50元充值卡','1000元超市购物券'],
    timer = 0,
    num = 0;
window.onload = function(){
  var title = document.getElementById('title'),
      play = document.getElementById('play'),
      stop = document.getElementById('stop');
 
  play.onclick = function(){
    playFun(title, play);
  };
  stop.onclick = function(){
    stopFun(play);
  };
  document.onkeyup = function(e){
    e = e || window.event;
    if(e.keyCode == 13){
      if(num === 0){
        playFun(title,play);
        num = 1;
      }else{
        stopFun(play);
        num = 0;
      }
    }
  }
};
 
function playFun(tit, node){
  clearInterval(timer);
  timer = setInterval(function(){
    tit.innerHTML = data[Math.floor(Math.random()* data.length)];
  },50);
  node.style.backgroundColor = '#999';
}
 
function stopFun(node){
  clearInterval(timer);
  node.style.backgroundColor = '#369';
}
原文地址:https://www.cnblogs.com/sunhw360/p/4139812.html