Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize the Lottery function of js

Shulou Source: shulou.com Published: 2022-06-03 10:22:50 10月05日 Update

This article is to share with you about how js implements the raffle function. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

1. Scene:

Click the button to start picking the prize at random, and when you stop that moment, what stays on the big screen is the prize ("thank you" means no prize).

two。 Train of thought:

As we all know, the lottery is mainly about randomness, and it is drawn from a bunch of awards. Then you can lock the direction: put the award name in an array and use Math.random () to return a random number between 0 (inclusive) and 1 (excluding). As long as you can randomly select the subscript of the array, you can randomly select the award.

Without saying much, attach the code:

Document starts to stop, please start var ostart=document.getElementById ('start'); var oend=document.getElementById (' end'); var otxt=document.getElementById ('txt'); var timer; var m Var list= ['cell phone', 'pad','pad',' shopping card', 'stereo', 'thank you', 'thank you' 'Thank you'] / / start ostart.onclick=function () {timer=setInterval (function () {/ / generate random number m=parseInt (Math.random () * list.length)) / / modify html otxt [XSS _ clean] = list [m] / modify the picture .setAttribute ('src',list [m])}, 1000)} / / stop oend.onclick=function () {clearInterval (timer); alert (' Congratulations on winning'+ list [m]); list.splice ('thank you');}

Click the start button:

Lucky draw result:

Thank you for reading! This is the end of the article on "how to realize the Lottery function of js". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Awards shopping cards shopping functions content prizes buttons arrays more articles random numbers extraction not bad practical well known subscript between code pictures scenes Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS MySQL Xiaomi vpn NVidia