最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue3按鈕點(diǎn)擊頻率控制的實(shí)現(xiàn)示例

 更新時(shí)間:2024年01月03日 15:38:27   作者:laizhenghua  
在前端開(kāi)發(fā)中,當(dāng)用戶(hù)頻繁連續(xù)點(diǎn)擊按鈕,可能會(huì)導(dǎo)致頻繁的請(qǐng)求或者觸發(fā)過(guò)多的操作,本文主要介紹了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)文章

最新評(píng)論

和平县| 运城市| 博白县| 乃东县| 长岭县| 连城县| 阿鲁科尔沁旗| 昌都县| 镇安县| 东乡| 南华县| 宜昌市| 湖州市| 永寿县| 永兴县| 南平市| 富源县| 彭水| 莫力| 元阳县| 临汾市| 江津市| 镇雄县| 龙江县| 宜都市| 永顺县| 怀化市| 巴塘县| 桃源县| 沙河市| 涞水县| 会宁县| 定远县| 讷河市| 克拉玛依市| 银川市| 南通市| 临猗县| 聂荣县| 荆州市| 清水河县|