使用:elementui、vue

例如:鼠标悬停到按钮上显示视频拉取中,在次悬浮到上面会频繁触发,所以限制3秒显示一次提示

可以使用纯 JavaScript 实现节流效果。以下是示例代码:

  1. 在组件中定义一个变量用于存储最近一次执行函数的时间戳:
export default {
  data() {
    return {
      lastExecTime: 0
    }
  },
  methods: {
    throttleGetSyncStatus(row) {
      const now = Date.now();
      if (now - this.lastExecTime >= 3000) {
        this.getSyncStatus(row);
        this.lastExecTime = now;
      }
    },
    getSyncStatus(row) {
      if (row.syncStatus === 2) {
      	this.$message.info("视频拉取中...");
      }
    },
  }
}
  1. el-button 标签中绑定 mouseenter 事件,同时调用 throttleGetSyncStatus 函数进行节流处理:
<el-button @mouseenter="throttleGetSyncStatus(row)" />

这样就能实现每 3 秒执行一次 getSyncStatus 函数的效果。