当前位置:网站首页>JS to realize simple lottery function
JS to realize simple lottery function
2022-06-30 14:32:00 【Domineering ape General Manager】
1. scene :
Click the button to start randomly drawing prizes , When it stops, what stays on the big screen is the prize (“ thank you ” No award ).
2. Ideas :
as everyone knows , The lottery is mainly about randomness , And it's a collection of awards . Then you can lock the direction : Use the array to award item names , use Math.random() To return between 0( contain ) ~ 1( It doesn't contain ) A random number between . As long as you can randomly select the subscript of the array , You can randomly select awards .
Don't talk much , Attach code :
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<button id="start"> Start </button>
<button id="end"> stop it </button>
<h1 id="txt"> Please start </h1>
<script> var ostart=document.getElementById('start'); var oend=document.getElementById('end'); var otxt=document.getElementById('txt'); var timer; var m; var list=[' mobile phone ','pad','pad',' Shopping card ',' Shopping card ',' Shopping card ',' sound ',' thank you ',' thank you ',' thank you ',' thank you ',' thank you ',' thank you ',' thank you ',' thank you ',' thank you '] // Start ostart.onclick=function(){
timer=setInterval(function(){
// Generate random numbers m=parseInt(Math.random()*list.length); // modify html otxt.innerHTML=list[m] // Modify picture .setAttribute('src',list[m]) },1000) } // stop it oend.onclick=function(){
clearInterval(timer); alert(' Congratulations on winning '+list[m]); list.splice(m,1,' thank you '); } </script>
</body>
</html>
Click on “ Start ” Button :
Lucky draw results :
Set the timer by yourself . Try it now !
边栏推荐
- Four isolation levels of MySQL
- [buuctf] [actf2020 freshman competition]include
- JS time conversion standard format, timestamp conversion standard format
- JMeter transaction controller
- Three uses of golang underscores
- XSS challenge (6-10) more detailed answers
- 深入理解.Net中的线程同步之构造模式(二)内核模式2.内核模式构造物Semaphone
- Go language func function
- Upgrade composer self update
- PHP 2D array change key name
猜你喜欢

Upgrade centos7 mysql5.5 to mysql5.7 non RPM in the form of tar package

Notes on reverse learning in the first week of winter vacation

Realize a simple LAN communication (similar to feiqiu)

Tencent two sides: @bean and @component are used on the same class. What happens?
![[scientific research data processing] [practice] frequency analysis chart of category variables, distribution chart of numerical variables and normality test (including lognormal)](/img/5a/eaa845f4332f0b8ee8b6409d6a79e8.png)
[scientific research data processing] [practice] frequency analysis chart of category variables, distribution chart of numerical variables and normality test (including lognormal)

"Persistent diseases" that cannot be solved in IM application development

About the problems encountered when using the timer class to stop with a button (why does the QPushButton (for the first time) need to be clicked twice to respond?)

V3 03_ Getting started

remote: Support for password authentication was removed on August 13, 2021. Please use a personal ac

1 figure to explain the difference and connection between nodejs and JS
随机推荐
Shell programming overview
Summary of use of laravel DCAT admin
ctfshow nodejs
深入理解.Net中的线程同步之构造模式(二)内核模式4.内核模式构造物的总结
1 figure to explain the difference and connection between nodejs and JS
Att & CK red team evaluation field (I)
Not satisfied with markdown native code block style? Try this beautify code screenshot tool~~
Solve the error in my QT_ thread_ global_ End(): 3 threads didn't exit
【Redis 系列】redis 学习十六,redis 字典(map) 及其核心编码结构
Google Earth engine (GEE) -- converts string to number and applies it to time search (ee.date.fromymd)
Use of laravel repository mode
我想问一下招商证券怎么开户?通过链接办理股票开户安全吗
When SQL queries are performed in table storage, an error is reported when the primary key is added to the query result, and the query result exceeds 10W rows. Do you want to add multiple indexes to t
Mutex lock, read / write lock, spin lock, pessimistic lock, and optimistic lock
【 scientific literature measurement 】 mining and visualization of keywords in foreign and Chinese Literature
org. json. The jsonobject object is converted to JSON, and JSON adds new elements. The value is obtained according to the JSON key. And list object format string to jsonarray
For loop and promise to solve the problem of concurrent callback
[buuctf] [actf2020 freshman competition]exec1
深入理解.Net中的线程同步之构造模式(二)内核模式3.内核模式构造物Mutex
Pit used by go language array type