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:
this.allowed = false;
will be called until the API call completes, allowing you to send more spam during this time. Set it tofalse
immediately after validatingif(this.allowed)
.