当从方法中修改监视变量时,使用监视程序的 Vue 倒计时器会加速

发布于 2025-01-11 11:14:08 字数 1104 浏览 0 评论 0 原文

对 Vue 和 JS 非常陌生。我已经为 timerCount 变量(最初设置为 5)设置了一个观察器,它创建了一个 5 秒的计时器。当变量达到 0 时,会执行一些代码,然后我将计时器重置为 5 以重新启动它。这工作得很好,但是,我有一个调用方法的单击事件,该方法将执行不同的代码,然后将计时器重置为 5,但现在我的计时器加速了(快两倍)。

从我通过谷歌搜索可以发现,似乎有多个观察器/计时器实例同时运行,这就是导致加速的原因。我该如何解决这个问题,以便我的方法只是像平常一样重置计时器?

watch: {
    timerCount: {
        handler(value){
            //timer for 5 seconds
            if (value>0){
                setTimeout(() => {
                    this.timerCount--;
                }, 1000);
            }
            //if timer hits 0, execute code and reset timerCount to 5 seconds, this works fine
            else{
                /* Code */
                this.timerCount=5
            }
        },
        immediate: true,
    }
},

methods:{
    //this resets the timer, but it now goes twice as fast, don't know why.
    otherMethod(){
        /* Code */
        this.timerCount=5
    }

有什么

帮助吗?

这是我得到此代码的帖子: 如何在 Vue.js 中创建一个简单的 10 秒倒计时

very new to Vue and JS. I've setup a watcher for a timerCount variable (initially set to 5) which makes a 5 second timer. When the variable hits 0, some code is executed, and I reset the timer to 5 to restart it. This works perfectly fine, however, I have a click event which calls a method, which will execute different code and then reset the timer to 5 as well, but now my timer is accelerated (twice as fast).

From what I could find from googling, it seems that there are multiple watcher/timer instances running at the same time, which is what causes the speed up. How do I fix this so my method simply reset the timer like normal?

watch: {
    timerCount: {
        handler(value){
            //timer for 5 seconds
            if (value>0){
                setTimeout(() => {
                    this.timerCount--;
                }, 1000);
            }
            //if timer hits 0, execute code and reset timerCount to 5 seconds, this works fine
            else{
                /* Code */
                this.timerCount=5
            }
        },
        immediate: true,
    }
},

methods:{
    //this resets the timer, but it now goes twice as fast, don't know why.
    otherMethod(){
        /* Code */
        this.timerCount=5
    }

}

Any help?

Here is the post where I got this code: How do I create a simple 10 seconds countdown in Vue.js

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

青柠芒果 2025-01-18 11:14:08

在您的代码中,您正在观看timerCount。当你对 timeCount 进行更改时,意味着当你运行 otherMethod 时,Vue 会监视它,然后再次运行观察程序,并且该观察程序的处理程序再次运行。每次你改变timerCount变量,你的观察者就会一次又一次地运行。

实际上,如果没有观察者,您可以使用 setInterval (而不是 setTimeout)在创建的事件中开始计时。 setInterval 在给定的时间间隔内运行您的代码,但不严格为 1000 毫秒。它可能是 1005ms 或更短。

您可以在 setInterval 中创建一些函数,并给它 100ms 之类的值,并控制时间是否超过 5 秒。

In you code, you are watching timerCount. When you make a change on your timeCount means, when you run otherMethod, Vue watches it then run watcher again and handler for this watcher runs again. Everytime you change timerCount variable your watcher run and again and again.

Actually without watcher you can start your time inside your created event with setInterval (not setTimeout). setInterval run your code within givin interval but not strictly 1000ms. It may be 1005ms or less.

You can create some function inside setInterval and give it like 100ms and control the time if it is passed 5 seconds or not.

昔梦 2025-01-18 11:14:08

创建一个方法 (createWatcher()),用于初始化观察程序并将观察程序实例保存为数据变量。创建组件时调用createWatcher(),每次需要修改监视变量时,停止监视程序并更新变量,然后再次创建监视程序。 (参见 whenUpdatingVariable())

参考创建watcher< /a> 和 停止观察者

data(){
 return{
  myWatcherInstance: null,
  timerCount: 10
 }
}
methods: {
 whenUpdatingVariable(){
  // stop watcher/unwatch
  this.myWatcherInstance();

  // update variable
  this.timerCount = 20;

  // create watcher again
  this.createWatcher();
 },
 createWatcher(){
    this.myWatcherInstance = this.$watch('timerCount', () => {
        setTimeout(() => {
          this.timerCount--;
        }, 1000);
      })
    }
  }
},
created(){
 this.createWatcher();
}

Create a method (createWatcher()) that initializes a watcher and saves the watcher instance as a data variable. Call the createWatcher() when the component is created and each time you need to modify the watched variable, stop the watcher and update the variable then create the watcher again. (see whenUpdatingVariable())

Refer to creating watcher and stopping watcher

data(){
 return{
  myWatcherInstance: null,
  timerCount: 10
 }
}
methods: {
 whenUpdatingVariable(){
  // stop watcher/unwatch
  this.myWatcherInstance();

  // update variable
  this.timerCount = 20;

  // create watcher again
  this.createWatcher();
 },
 createWatcher(){
    this.myWatcherInstance = this.$watch('timerCount', () => {
        setTimeout(() => {
          this.timerCount--;
        }, 1000);
      })
    }
  }
},
created(){
 this.createWatcher();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文