vue3按鈕點(diǎn)擊頻率控制的實(shí)現(xiàn)示例
現(xiàn)有一個按鈕,如下圖
點(diǎn)擊時
再次點(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' // 國際化配置 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)擊過于頻繁,請${Math.floor(diff)}秒后嘗試!`, type: 'warning', }) return } // 執(zhí)行按鈕功能(處理業(yè)務(wù)邏輯) handleAlert() } // 封裝可以設(shè)置過期時間的localStorage const setExpiredItem = (key, value, expires) => { const data = { value: value, expires: Date.now() + expires } window.localStorage.setItem(key, JSON.stringify(data)) } // 封裝獲取有過期時間的localStorage(我們規(guī)定如果key到期則返回-1,如果沒有這個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)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
遇到vue前端npm?i報(bào)錯多個版本不一致問題及解決
這篇文章主要介紹了遇到vue前端npm?i報(bào)錯多個版本不一致問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-03-03vue store之狀態(tài)管理模式的詳細(xì)介紹
這篇文章主要介紹了vue store之狀態(tài)管理模式的詳細(xì)介紹,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06Vue.js 遞歸組件實(shí)現(xiàn)樹形菜單(實(shí)例分享)
本文主要對介紹利用Vue.js 的遞歸組件,實(shí)現(xiàn)了一個最基本的樹形菜單。具有很好的參考價(jià)值,下面就跟著小編一起來看下吧2016-12-12在vue項(xiàng)目中使用Nprogress.js進(jìn)度條的方法
NProgress.js是輕量級的進(jìn)度條組件,使用簡便,可以很方便集成到單頁面應(yīng)用中。這篇文章主要介紹了在vue項(xiàng)目中使用Nprogress.js進(jìn)度條的方法,需要的朋友可以參考下2018-01-01Vue3 + TypeScript 開發(fā)總結(jié)
本文直接上 Vue3 + TypeScript + Element Plus 開發(fā)的內(nèi)容,感興趣的話一起來看看吧2021-08-08