vue3按鈕點(diǎn)擊頻率控制的實(shí)現(xiàn)示例
現(xiàn)有一個(gè)按鈕,如下圖

點(diǎn)擊時(shí)

再次點(diǎn)擊

刷新窗口再次點(diǎn)擊

刷新窗口依然可以實(shí)現(xiàn)點(diǎn)擊頻率控制。
代碼實(shí)現(xiàn):
<template>
<!--<el-config-provider :locale="locale">
<router-view/>
</el-config-provider>-->
<el-button type="primary" @click="handleClick">click</el-button>
</template>
<script setup lang="ts">
// @ts-ignore
import zhCn from 'element-plus/dist/locale/zh-cn.mjs'
import { reactive } from 'vue'
import { ElMessage } from 'element-plus'
// 國(guó)際化配置
const locale = reactive(zhCn)
const handleClick = () => {
// 間隔為30s
const delay = 30000
const key = 'ls_handle_click'
let cache = getExpiredItem(key)
if (cache <= 0) {
setExpiredItem(key, Date.now(), delay)
} else {
const now = Date.now()
const diff = delay / 1000 - (now - cache) / 1000
ElMessage({
showClose: true,
message: `點(diǎn)擊過(guò)于頻繁,請(qǐng)${Math.floor(diff)}秒后嘗試!`,
type: 'warning',
})
return
}
// 執(zhí)行按鈕功能(處理業(yè)務(wù)邏輯)
handleAlert()
}
// 封裝可以設(shè)置過(guò)期時(shí)間的localStorage
const setExpiredItem = (key, value, expires) => {
const data = {
value: value,
expires: Date.now() + expires
}
window.localStorage.setItem(key, JSON.stringify(data))
}
// 封裝獲取有過(guò)期時(shí)間的localStorage(我們規(guī)定如果key到期則返回-1,如果沒(méi)有這個(gè)key就返回0)
const getExpiredItem = (key) => {
const value = window.localStorage.getItem(key)
if (!value) {
return 0
}
const data = JSON.parse(value)
if (Date.now() > data.expires) {
window.localStorage.removeItem(key)
return -1
}
return data.value
}
// 按鈕功能
const handleAlert = () => {
ElMessage({
showClose: true,
message: 'this is a success message.',
type: 'success',
})
}
</script>
<style scoped lang="scss">
</style>
到此這篇關(guān)于vue3按鈕點(diǎn)擊頻率控制的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)vue3點(diǎn)擊頻率控制內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue.js父子組件傳參的原理與實(shí)現(xiàn)方法
這篇文章主要介紹了vue.js父子組件傳參的原理與實(shí)現(xiàn)方法,結(jié)合實(shí)例形式分析了vue.js父子組件傳參的基本原理、實(shí)現(xiàn)方法與相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2023-04-04
Vue3中關(guān)于路由規(guī)則的props配置方式
這篇文章主要介紹了Vue3中關(guān)于路由規(guī)則的props配置方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-03-03
vue之this.$router.push頁(yè)面刷新問(wèn)題
這篇文章主要介紹了vue之this.$router.push頁(yè)面刷新問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
vue中手機(jī)號(hào),郵箱正則驗(yàn)證以及60s發(fā)送驗(yàn)證碼的實(shí)例
下面小編就為大家分享一篇vue中手機(jī)號(hào),郵箱正則驗(yàn)證以及60s發(fā)送驗(yàn)證碼的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-03-03
基于vue中對(duì)鼠標(biāo)劃過(guò)事件的處理方式詳解
今天小編就為大家分享一篇基于vue中對(duì)鼠標(biāo)劃過(guò)事件的處理方式詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08

