JavaScript to implement keyboard-controlled lottery system _javascript skills

Source: Internet
Author: User

Make a simple lottery system! Welcome everybody to study!

JS principle: Set up an array to store lottery content, such as Iphone6, when the click Start, open the timer, generate a random number, the corresponding text of the innerHTML to the corresponding contents of the group.

If you want to make a lucky draw more likely, you can let a value in the array repeat more times. Next look at the code. 、

JavaScript code

Window.onload = function () {var data = ["iphone 6s Plus", "Apple Mac Notebook", "Beauty Washing Machine", "Ling Mei Pen", "Thank you for participating", "Sony ear-type Headset", "Leibo mechanical Keyboard", "JavaScript Advanced Program Design", "exquisite Thermos flask", "Pooh Bear One", "500 yuan Chinese petrochemical Refueling card", "Archie Art annual fee member", "IPa
  D mini "," 32G u Disk ",];
  var bstop = true;
  var timer = null;
  var btns = document.getElementById (' Btns '). getElementsByTagName (' span ');

  var text = document.getElementById (' text ');
  Btns[0].onclick = start;

  Btns[1].onclick = stop; Document.onkeyup = function (event) {event = event| |
    window.event;
      if (event.keycode==13) {if (bstop) {start ();
      }else {stop ();
    }} function Start () {clearinterval (timer);
      Timer = setinterval (function () {var r = Math.floor (Math.random () *data.length);
    text.innerhtml = Data[r];
    },20);
    Btns[0].style.background = ' #666 ';
  Bstop = false;
    function Stop () {clearinterval (timer);
      Btns[0].style.background = ' Blue ';Bstop = true;

 }
}

HTML CSS code:

<! DOCTYPE html>  

I hope this article will help you learn, easy to achieve the lottery system.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.