A
A
aweui2019-01-12 22:14:46
Vue.js
aweui, 2019-01-12 22:14:46

Execute method after n-interval of time, but only after first event?

Hey!
There is an @click event that calls the doSomething() method .
The goal is to let the method execute immediately, but only on the first time/click. And in subsequent times / clicks, execute this method through setInterval.
How to implement it?
PS: I'm intricate, of course ...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Sklyarov, 2019-01-12
@aweui

Create a flag, set it to false, change it to true on the first click, and start the interval.
more or less like this:

data(){
      return {
        flag: false,
      }
    },
    methods: {
      somefunc(){
            if(this.flag == false) {
               //do something
              //то что выполнится в первый раз
            } else {
               setInterval(func,5000);
               // то что будет выполняться следующие разы
            }
            this.flag = true;
            
           
        }
      },

It won't work for you the first time. And then something will be executed at intervals. But this is the first thing that came to mind, I think the solution is quite crutch.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question