VueJS axios allows to click button only once and allows second click after receiving data
P粉938936304
P粉938936304 2024-02-26 11:44:47
0
2
442

I'm working on a like/dislike system on Laravel/VueJS.

My system works, but I want to avoid spammers.

Like button:

<a v-on:click="like(10, $event)">
    <i class="fas fa-heart"></i>
</a>

10 is the post id, which is generated in laravel Blade...

This is how I try to avoid spammers:

const app = new Vue({
el: '#app',
data() {
    return {
        allowed: true,
    };
},
methods: {
    like: function (id, event) {
        if (this.allowed) {
            axios.post('/posts/' + id + '/like', {  
                post_id: id,
            })
            .then((response) => {
                this.allowed = false; //Set allowed to false, to avoid spammers.

                ..... code which changes fa-heart, changes class names, texts etc ....

                // send notification to user
                Vue.toasted.show('Bla bla bla successfuly liked post', {
                    duration: 2000,
                    onComplete: (function () {
                        this.allowed = true //After notification ended, user gets permission to like/dislike again.
                    })
                });

But something is missing here, or I'm doing something wrong. When I click on a similar icon very very quickly and check the requests, axios sends 3-4-5 requests (depending on how fast you click)

Only after 3-5 requests data.allowed will become false. Why? I want:

  1. allow=true;
  2. User click -> allowed = false; >Second click "You cannot click again because the previous notification has not ended yet";
  3. End of last notification -> allowed = true;
  4. ...cycle

P粉938936304
P粉938936304

reply all(2)
P粉752479467

this.allowed = false; will be called until the API call completes, allowing you to send more spam during this time. Set it to false immediately after validating if(this.allowed).

if (this.allowed) {
    this.allowed = false; // Then do the call
}
P粉477369269
    like: function (id, event) {
        // first, check if the `like` can be sent to server
        if (!this.allowed) return;
        // remember that we are sending request, not allowed to `like` again
        this.allowed = false;

        var self = this;  // you need this to remember real this
        axios.post('/posts/' + id + '/like', {  
            post_id: id,
        }).then((response) => {
            ..... code ....

            // send notification to user
            Vue.toasted.show('Bla bla bla successfuly liked post', {
                duration: 2000,
                onComplete: function () {
                    //After notification ended, user gets permission to like/dislike again.
                    self.allowed = true;
                }
            );
       }).catch(function() {
            // maybe you also need this catch, in case network error occurs
            self.allowed = true;
       })
        ....
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!