Vue + Element 实现按钮指定间隔时间点击
  ov3h1y5Ql0rU 2023年12月07日 29 0

1、业务需求

需要加一个按钮,调用第三方API,按钮十分钟之内只能点击一次,刷新页面也只能点击一次

2、思路

加一个本地缓存的时间戳,通过时间戳计算指定时间内不能点击按钮

3、实现

1)vue页面

<template>
  <el-row :gutter="15">
    <el-col :span="4">
      <el-button
      type="danger"
      icon="el-icon-download" 
      @click="getData"
      :loading="getDataLoading">获取数据</el-button>
    </el-col>
  </el-row>
</template>

<script type="text/ecmascript-6">
import { GetDataInfo } from '@/api/xxx'

export default {
  data () {
    return {
      getDataLoading: false,
    }
  },
  methods: {
    // 获取数据按钮,10分钟内执行一次(本地缓存)
    async getData () {
      const storedTime = localStorage.getItem('lastClickGetDataTime') 
      const currentTime = Date.now() // 时间戳(秒级)
      if (!storedTime || (currentTime - storedTime) / 1000 / 60 >= 10) {
        // 如果存储的时间不存在或者距离上次点击时间超过10分钟,则执行按钮点击操作  
        this.getDataLoading = true
        try {
          await GetDataInfo({})
        } catch (error) {
          this.getDataLoading = false
        }
        this.getDataLoading = false
        localStorage.setItem('lastClickGetDataTime', currentTime) 
      } else {  
        // 距离上次点击时间小于10分钟,不做任何操作或给出提示  
        this.$message({  
          message: '请在十分钟后再点击按钮',  
          type: 'warning',
        })
      }
    },
  },
}
</script>

// 注:指定时间可以根据需要更新,比如1分钟内只能点击一次,只需要将循环部分改为

if (!storedTime || (currentTime - storedTime) / 1000 >= 60)

2) 效果

Vue + Element 实现按钮指定间隔时间点击_element


希望以上内容能够帮助你使用Vue + Element 实现按钮指定间隔时间点击。欢迎点赞、关注、收藏,如果你还有其他问题,欢迎评论区交流。

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2023年12月07日 0

暂无评论

推荐阅读
  gYl4rku9YpWY   2024年05月17日   57   0   0 Vue
  JZjRRktyDDvK   2024年05月02日   79   0   0 Vue
  VlNAKfyhjjp9   2024年04月30日   76   0   0 Vue
  JNTrZmaOQEcq   2024年04月30日   54   0   0 Vue
  onf2Mh1AWJAW   2024年05月17日   59   0   0 Vue
  3A8RnFxQCDqM   2024年05月17日   58   0   0 Vue
ov3h1y5Ql0rU