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

相关推荐

  1. WPF 防止按钮Click时间多次响应

    2023-12-08 08:42:03       24 阅读
  2. 【CSS节流】实现防止按钮重复

    2023-12-08 08:42:03       41 阅读
  3. 如何实现vue按钮进行图片浏览 ?

    2023-12-08 08:42:03       38 阅读
  4. 实用Python定时Chrome网页按钮

    2023-12-08 08:42:03       37 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-08 08:42:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-08 08:42:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-08 08:42:03       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-08 08:42:03       20 阅读

热门阅读

  1. vue3在table里使用elementUI的form表单验证

    2023-12-08 08:42:03       40 阅读
  2. Boost:asio单io_service,多线程run

    2023-12-08 08:42:03       28 阅读
  3. C++设计模式学习之一(共计13种)

    2023-12-08 08:42:03       32 阅读
  4. LISP~~~~~

    2023-12-08 08:42:03       37 阅读
  5. Spring Kafka常用配置详解

    2023-12-08 08:42:03       35 阅读
  6. c++通过serial库进行上下位机通信

    2023-12-08 08:42:03       40 阅读
  7. 怎样学习AI编程?

    2023-12-08 08:42:03       38 阅读