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

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 实现按钮指定间隔时间点击。欢迎点赞、关注、收藏,如果你还有其他问题,欢迎评论区交流。

我的博客即将同步至腾讯云开发者社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=p7n9tysxbmek

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

GoodTimeGGB

鼓励一下!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值