Answer the question
In order to leave comments, you need to log in
How to apply to change a variable from an ajax function?
When the button is clicked, an Ajax request is sent to the server. While it is running, being processed, etc., the same function should not be executed again. Those. so that the button becomes disabled at the time of Ajax operation. And when the answer comes - activated again. It seems like a trivial task, but there were difficulties. Here is the code:
data: {
enable: true
},
sendMessage: function () {
if( this.enable === true ) {
this.enable = false;
window.axios.defaults.headers.common['X-CSRF-TOKEN'] = $('meta[name="csrf-token"]').attr('content');
axios.post('/url', {
})
.then(function (res) {
this.enable = true;
})
.catch(function (error) {
console.error(error);
});
Answer the question
In order to leave comments, you need to log in
The problem is that a function has its own scope and this inside that function is not the same this as above.
Replace
.then(function (res) {
this.enable = true;
})
.then(res => {
this.enable = true;
})
And better use .finally for axios. Otherwise, you will have to write the button unlock twice.
https://developer.mozilla.org/en/docs/Web/JavaScript...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question