由于用户的网络环境等各种原因,防止按钮重复点击,表单重复提交等都是减少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秒只会执行一次按钮点击应该执行的事情):
结语
- 本文的例子思路和实现都是相当简单的,但确是很实用的方法。
- 另外,从这个思路出发,还能解决一些类似的问题,比如:
- 音乐进度条的拖动。由于拖动的报告率太高,导致拖动过程界面卡顿,音乐的播放也很卡顿,那就可以用这个思路,每一段时间内只对应响应一次。
- 降低输入框的响应。
- 后续我们再聊聊上面这些例子的解决方法实现。他们本质都是这个模拟加锁的思路,但可能有些需要一些细节上不同的处理。
mexican drugstore online: mexico drug stores pharmacies – medicine in mexico pharmacies
оборудование переговорных комнат оборудование переговорных комнат .
mexican online pharmacies prescription drugs: п»їbest mexican online pharmacies – pharmacies in mexico that ship to usa
buying from online mexican pharmacy: medication from mexico pharmacy – mexican pharmaceuticals online
purchase terazosin for sale – generic tamsulosin 0.4mg priligy 90mg canada
buy speman generic – order finasteride sale finasteride online order
buying prescription drugs in mexico online: buying from online mexican pharmacy – best online pharmacies in mexico
mexican mail order pharmacies: mexican pharmaceuticals online – buying prescription drugs in mexico
reputable mexican pharmacies online: reputable mexican pharmacies online – mexico drug stores pharmacies
mexico drug stores pharmacies: mexican drugstore online – best online pharmacies in mexico
medication from mexico pharmacy: buying prescription drugs in mexico – buying prescription drugs in mexico
mexico pharmacy buying prescription drugs in mexico online mexican online pharmacies prescription drugs
reputable mexican pharmacies online: reputable mexican pharmacies online – buying from online mexican pharmacy
mexican online pharmacies prescription drugs mexico pharmacies prescription drugs mexican online pharmacies prescription drugs
mexico pharmacies prescription drugs: buying prescription drugs in mexico – reputable mexican pharmacies online
mexico drug stores pharmacies: mexican mail order pharmacies – reputable mexican pharmacies online
baclofen buy
mexico pharmacies prescription drugs: mexico drug stores pharmacies – mexico drug stores pharmacies
Здравствуйте!
Приобрести диплом университета.
lastdemo.primepix.ru/club/user/88/blog/8745
mexican online pharmacies prescription drugs: mexico drug stores pharmacies – mexican online pharmacies prescription drugs
purple pharmacy mexico price list mexican rx online mexican border pharmacies shipping to usa
Добрый день!
Заказать документ о получении высшего образования можно у нас в Москве.
diploms-x.com/kupit-diplom-kandidata-nauk
Привет!
Сколько стоит диплом высшего и среднего образования и как его получить?
theplaygamepicks.com/диплом-техникума-где-и-как-купить
Рады оказать помощь!.
Привет!
Заказать документ университета можно в нашей компании.
asxdiplomik.com/kupit-diplom-chelyabinsk
Привет!
Официальная покупка диплома вуза с упрощенной программой обучения
hi-fi-forum.net/profile/957272
Рады помочь!.
Добрый день!
Мы готовы предложить дипломы любых профессий по приятным тарифам.
asromafansclub.com/read-blog/5917_nadezhnyj-magazin-s-obshirnym-assortimentom-dokumentov
mexico drug stores pharmacies: medication from mexico pharmacy – purple pharmacy mexico price list
mexican pharmaceuticals online mexican rx online best online pharmacies in mexico
mexico pharmacies prescription drugs: buying prescription drugs in mexico – buying from online mexican pharmacy
purchase ventolin inhaler
medication from mexico pharmacy: mexican pharmaceuticals online – best online pharmacies in mexico
mexican mail order pharmacies: buying prescription drugs in mexico – medication from mexico pharmacy
mexico drug stores pharmacies: buying prescription drugs in mexico online – pharmacies in mexico that ship to usa
mexican pharmaceuticals online: pharmacies in mexico that ship to usa – п»їbest mexican online pharmacies
п»їbest mexican online pharmacies: mexican border pharmacies shipping to usa – buying prescription drugs in mexico
pharmacies in mexico that ship to usa: pharmacies in mexico that ship to usa – п»їbest mexican online pharmacies
п»їbest mexican online pharmacies: mexican pharmaceuticals online – mexican pharmaceuticals online
mexican mail order pharmacies: п»їbest mexican online pharmacies – mexican mail order pharmacies
mexican border pharmacies shipping to usa buying prescription drugs in mexico online mexico pharmacies prescription drugs
medication from mexico pharmacy buying from online mexican pharmacy buying from online mexican pharmacy
reputable mexican pharmacies online: mexican pharmaceuticals online – mexican online pharmacies prescription drugs
purple pharmacy mexico price list buying from online mexican pharmacy mexican rx online
pharmacies in mexico that ship to usa: mexico pharmacies prescription drugs – buying prescription drugs in mexico online
mexico pharmacies prescription drugs: medicine in mexico pharmacies – medicine in mexico pharmacies
[url=https://rentyachtsincyprus.com/en]Charter a yacht and experience the ultimate in luxury and convenience for your next special occasion or private vacation in Cyprus. Our highly trained crew is dedicated to ensuring your safety and wellbeing while providing exceptional service that caters to your every need. With our yachts maintained to the highest standards, you can relax and enjoy your trouble-free journey in style and comfort. Let us help you create unforgettable memories on the crystal-clear waters of your dream destination in Cyprus.[/url]
[url=https://rentyachtsincyprus.com/]For lovers of outdoor activities and sunbathing by the pool, renting a yacht is the best solution, because while sunbathing in the bow of a yacht, you can simultaneously explore the many marine attractions located along the entire coast of Cyprus.[/url]
[url=https://rentyachtsincyprus.com/]Extraordinarily beautiful sunrises and mysterious sunsets, vivid emotions from bewitching landscapes, exciting trails along the coastline of the island and lots of excitement from a boat trip.[/url]
[url=https://rentyachtsincyprus.com/]Our company “Rent Yachts in Cyprus” provides yacht rental services in Cyprus. With our extensive fleet of yachts, we offer a variety of options for every taste and budget. Our team of experienced professionals is dedicated to providing exceptional service and ensuring that our customers have an unforgettable experience on the beautiful waters of Cyprus. Book your yacht rental with us and discover the stunning coastline of Cyprus in style and comfort.[/url]
Привет!
Приобрести диплом любого университета.
jagplay.ekafe.ru/viewtopic.php?f=22&t=4149
Привет!
Заказать диплом ВУЗа.
entrastart.com//read-blog/46
buy generic clomid online
mexican online pharmacies prescription drugs: pharmacies in mexico that ship to usa – п»їbest mexican online pharmacies
sildalis 120