Vue系列:vue element ui中Progress怎么通过定时器自动加载一个数组中的值,播放的时候可暂停,播放完成停止

27 篇文章 0 订阅

要使用Vue Element UI中的Progress组件自动加载数组中的值,可以通过以下步骤实现:

1. 创建一个名为“progressValue”的响应式数据属性,并将其初始化为0。这个属性将用于更新Progress组件的进度值。
2. 在mounted生命周期钩子函数中,启动一个定时器来递增“progressValue”属性。每当定时器触发时,“progressValue”属性将增加一些数量。这里需要注意,定时器需要在组件销毁时进行清理,以避免内存泄漏。
3. 创建一个计算属性,根据“progressValue”属性和数组长度计算出当前播放的百分比。这个计算属性将用于更新Progress组件的百分比显示。
4. 将Progress组件添加到模板中,并将百分比绑定到计算属性中。此外,可以添加一个按钮或其他控件来启动/暂停定时器。

以下是示例代码:

<template>
  <div>
    <el-progress :percentage="percentage"></el-progress>
    <el-button @click="toggleTimer">{{ timerRunning ? 'Pause' : 'Play' }}</el-button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      progressValue: 0,
      timer: null,
      timerRunning: false,
      data: [/* your array of values */]
    };
  },
  mounted() {
    this.timer = setInterval(() => {
      if (this.progressValue >= this.data.length) {
        // Stop the timer when we reach the end of the array
        this.stopTimer();
      } else {
        // Increment the progress value
        this.progressValue++;
      }
    }, 1000);
  },
  computed: {
    percentage() {
      return Math.round((this.progressValue / this.data.length) * 100);
    }
  },
  methods: {
    toggleTimer() {
      this.timerRunning ? this.stopTimer() : this.startTimer();
    },
    startTimer() {
      this.timerRunning = true;
    },
    stopTimer() {
      clearInterval(this.timer);
      this.timerRunning = false;
    }
  },
  beforeDestroy() {
    // Cleanup the timer to avoid memory leaks
    this.stopTimer();
  }
};
</script>

在这个例子中,我们创建了一个名为“data”的数组,用于存储需要播放的值。在组件加载时,我们启动了一个定时器,每秒钟递增“progressValue”属性。我们还定义了一些方法来启动/暂停定时器,并在组件销毁时清理定时器。最后,我们使用Vue指令将Progress组件的百分比绑定到计算属性“percentage”中。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

浪潮行舟

你的鼓励是我创作的最大动力。

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

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

打赏作者

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

抵扣说明:

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

余额充值