V
V
Vladislav Dudashvili2021-05-31 22:46:23
Vue.js
Vladislav Dudashvili, 2021-05-31 22:46:23

How can I make the method be called with a delay?

Guys, tell me how to solve the problem. I need this method to be called only after 2 seconds, and calls are not queued after each click on the handle that pulls the request. I tried to do it through setTimeout, but all the same, requests are queued. Those. it is necessary that after the button is pressed, time passes and the result is displayed, and if the person presses the button again, then only after 2 seconds there would be the next call, but the requests would not be queued, but there was only one. I hope I explained more or less. I read what can be done through debounce, but I don’t understand how I can then build the logic..
I try like this, but I get an error. I don't understand how exactly to do this..
error Move function declaration to function body root no-inner-declarations

Here is the method to be wrapped in a debounce with a 2 second delay.

async getStatisctics_actions({ commit }) {
            try {
                const url = '/recStructs'
                const options = {
                    params: {
                        currency: this.state.underlying, 
                        maturity: this.state.maturity, 
                        amount: this.state.amount, 
                        fut_hedge_flag: this.state.futHedgeFlag
                    }
                }
                const response = await axios.get(url, options)
                commit('setFullData_mutations', response.data.data) // Получаем все графики
            } catch (error) {
                return error
            }
        },


And here is what I tried to do and got an error.


async getStatisctics_actions({ commit }) { // Получаем все графики
      try {

        const debounce = (fn, ms) => {
            let timeout;
            return function() {
                const fnCall = () => { fn.apply(this, arguments)}
                clearTimeout(timeout);
                timeout = setTimeout(fnCall, ms)
            };
        }

        async function chartDebounce() {
            const url = "/recStructs";
            const options = {
            params: {
                currency: this.state.underlying,
                maturity: this.state.maturity,
                amount: this.state.amount,
                fut_hedge_flag: this.state.futHedgeFlag,
            },
            };
            const response = await axios.get(url, options);
            commit("setFullData_mutations", response.data.data); 
        }

      }
       catch (error) {
        return error;
      }
    },

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question