Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

285
Views
VueJS axios allow to click button only once, second time allow after data received

I am doing like/dislike system on Laravel/VueJS.

enter image description here

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 post id, it is generated in laravel blade...

This is what I tried to do 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 am doing something wrong. When I click very very fast on like icon, and check requests, axios sends 3-4-5 requests (depends how fast u click)

enter image description here

And only after that 3-5 requests data.allowed becomes false. Why? I want to:

  1. allowed = true;
  2. User clicks like -> allowed = false; > clicks second time "You are not allowed to click again, because previous notification not ended";
  3. Previous notification ended -> allowed = true;
  4. ... loop
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

The this.allowed = false; is beeing called until the API call finish so you can spam more within that time. Set it to false immediately after the comprobation if (this.allowed).

if (this.allowed) {
    this.allowed = false; // Then do the call
}
about 4 years ago · Juan Pablo Isaza Report

0

for me works, @click.once

<q-btn
@click.once ="selectItemFiles(options)"
/>

No matter how many times the user clicks, the action will be produced only one time

about 4 years ago · Juan Pablo Isaza Report

0

    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;
       })
        ....
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!