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:
I hope this article will help you learn, easy to achieve the lottery system.