**项目中有一个连续点击5次启用一个按钮的需求,心想这样的东西网上应该有很多实现
而后开始百度,结果找了很多连接,到那时最终都是一个样的(好似你转我而后我再转他),开始复制粘贴,发现用不了。。。最后看了下代码,发现还有定时器,仔细想一想这东西彻底不须要用定时器。
最后无奈本身写一个吧,顺便把代码贴出来,或许能帮到其余人。**this
lastTime: number = 0; count: number = 0; maxCount: number = 5; interval: number = 500; const date = new Date() if (date.getTime() - lastTime > interval) { this.lastTime = 0; } if (count === 0) { count++; lastTime = date.getTime(); return }; const currentInterval = date.getTime() - lastTime; if (currentInterval < interval) { if (count > maxCount - 1) { console.log(`是第五次了`); this.count = 0; this.lastTime = 0; return }; console.log(`还差${maxCount-count}下`); this.count++; this.lastTime = date.getTime(); return }; this.count = 0; this.lastTime = 0