A
A
aheles2021-11-03 09:32:47
Vue.js
aheles, 2021-11-03 09:32:47

Vue + axios, how to cancel a previous request if a new one has completed?

There is a button that sends a request to the server, which returns json. If a person presses the button many times, then there is an infinite number of requests, it is necessary that if a new request is called, then you need to interrupt the previous one and execute this (new)
axios itself:

getSchedules(locationId, gameModeId, selectedHeadsets, day) {

                let param = 'locationId=' + locationId + '&gameModeId=' + gameModeId + '&headsets=' + selectedHeadsets

                if (day)
                    param += '&day=' + day;

                return axios({
                    method: 'get',
                    url: '/schedules?' + param
                }).then((response) => {

                    this.schedules = response.data.schedules;
                    this.backDateWeek = response.data.backDateWeek;
                    this.nextDateWeek = response.data.nextDateWeek;
                    this.isAuth = response.data.auth;
                    this.requestPhone = response.data.requestPhone;

                    this.showDay = response.data.currentDay;

                    this.weeks = response.data.weeks;
                    this.selectedWeek = response.data.currentWeek;
                    this.account = response.data.account;
                    this.curDateEvent = response.data.cur_day

                    if(this.curDateEvent){
                        this.changeMobileDay(this.curDateEvent);
                    }

                }).catch((error) => {

                });
            },

Method call:
this.getSchedules(this.selectedLocationId, this.selectedGameModeId, this.selectedHeadsets,day).then(()=>{
                    document.getElementById('stepView4').scrollIntoView({behavior:'smooth'});
                });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Aetae, 2021-11-03
@Aetae

https://github.com/axios/axios#cancellation

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question