由于用户的网络环境等各种原因,防止按钮重复点击,表单重复提交等都是减少bug发生,降低服务器压力等相当有效的方法。下面来聊聊通过模拟我们在操作系统里类似加锁的思想来防止重复点击。
目录
思路
- 声明一个锁(布尔值),用来标识按钮,变量是否被锁上
- 当按钮按下后,把布尔值设置为false,即标记为已锁上
- 设置一个延时函数setTimeout(),等待一段时间后再把这个锁归还,设置布尔值为true
- 显然我们的按钮点击事件一开始就应该判断锁是否被锁上,是则直接结束。否则才能正常执行
示例
- HTML / JS
<div style="width:100%;display:flex;justify-content: center;background:#66ccff;border-radius:10px;border 1px solid #66ccff;">
<button onclick="btn_click()" style="border-radius:10px;">
点死我
</button>
</div>
<script>
//锁变量
let lock = true;
//按钮点击执行函数
function btn_click() {
if(lock == false) {
return;
}
//加锁
lock = false;
//执行按钮点击本应执行的事情
alert("点了一下我捏");
//限制间隔5000毫秒(即5秒)只能执行一次
setTimeout(function(){
//解锁
lock = true;
},5000);
}
</script>
- 效果(每隔5秒只会执行一次按钮点击应该执行的事情):
结语
- 本文的例子思路和实现都是相当简单的,但确是很实用的方法。
- 另外,从这个思路出发,还能解决一些类似的问题,比如:
- 音乐进度条的拖动。由于拖动的报告率太高,导致拖动过程界面卡顿,音乐的播放也很卡顿,那就可以用这个思路,每一段时间内只对应响应一次。
- 降低输入框的响应。
- 后续我们再聊聊上面这些例子的解决方法实现。他们本质都是这个模拟加锁的思路,但可能有些需要一些细节上不同的处理。
india pharmacy Generic Medicine India to USA mail order pharmacy india
http://indiaph24.store/# cheapest online pharmacy india
mexico pharmacy: Online Pharmacies in Mexico – mexico pharmacies prescription drugs
cheapest pharmacy to fill prescriptions without insurance
online pharmacy birth control pills
best canadian pharmacy to order from Large Selection of Medications from Canada canadian pharmacy no rx needed
synthroid 112 mcg
buy valtrex
https://mexicoph24.life/# best online pharmacies in mexico
mexico drug stores pharmacies: mexican pharmacy – п»їbest mexican online pharmacies
https://mexicoph24.life/# best online pharmacies in mexico
tadalafil india 10mg
mexican rx online: mexico pharmacy – mexican rx online
http://mexicoph24.life/# mexican drugstore online
https://mexicoph24.life/# best mexican online pharmacies
https://canadaph24.pro/# canadian pharmacy 365
best online pharmacies in mexico Mexican Pharmacy Online mexico pharmacy
generic lisinopril 5 mg
http://canadaph24.pro/# best canadian pharmacy online
india online pharmacy Online medicine order top online pharmacy india
can i purchase valtrex over the counter
medicine prednisone 5mg
canadian pharmacies comparison
can i buy valtrex online
best rated canadian pharmacy: canadian pharmacy antibiotics – canadadrugpharmacy com
mexico drug stores pharmacies mexican pharmacy buying from online mexican pharmacy
https://canadaph24.pro/# canadianpharmacymeds
metformin 1 00 mg
http://canadaph24.pro/# best canadian online pharmacy
tadalafil price in india
Experience Excellence with Bwer Pipes: Elevate your farming operations with Bwer Pipes’ industry-leading irrigation solutions. Our cutting-edge sprinkler technology and durable pipes are engineered for superior performance, enabling Iraqi farmers to achieve optimal water management, crop health, and productivity. Explore Bwer Pipes
buying cialis online
no prescription required pharmacy
metformin 500 mg tabs
ketoconazole over the counter – order ketoconazole 200mg online itraconazole 100 mg generic
запись к психотерапевту https://w-495.ru/
best online canadian pharmacy Large Selection of Medications from Canada canadian pharmacy cheap
valtrex drug
precription drugs from canada legal to buy prescription drugs from canada canadian neighbor pharmacy
https://canadaph24.pro/# best canadian pharmacy to order from
запись к психотерапевту https://w-495.ru/
metformin online purchase uk
oral nizoral 200 mg – order itraconazole pills buy itraconazole paypal
buy zithromax 1000mg online
online pharmacy
online pharmacy uk
synthroid 200 mg
best india pharmacy
canadadrugpharmacy com
https://mexicoph24.life/# buying prescription drugs in mexico