Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

118
Vistas
How to make delay in delete request for 1 sec in Vue.js

I have products that are being taken with API calls and the user can put them into the cart. So, they can be also deleted but I want it to be removed from the front-end after 1 sec because there is an animation on the delete button. So my cart is like the one below:

enter image description here

So when the bin button clicked it is being deleted but immediately, I want it to be taken like 1 or 2 sec. So for this my code is like:

<template>
    <div id="delete-button"  @click.prevent="removeProductFromCart(item.id)">
        <input type="checkbox" id="checkbox">
        <div id="bin-icon">
            <div id="lid"></div>
            <div id="box">
                <div id="box-inner">
                    <div id="bin-lines"></div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import cartHelper from "../helpers/cartHelper";

export default {
    props: {
        item: Object,
    },
    data() {
        return {
            loading: false,
        };
    },
    methods: {
        removeProductFromCart(id) {
            cartHelper.removeFromCart(id, (response) => {
                this.loading = true;
                this.$store.dispatch('removeProductFromCart', {
                    cart: response.data,
                })
                setTimeout(() => this.loading = false, 1000);
            });
        },
    }
};
</script>

So I don't know even I set a timeout why it is being deleted immediately but not wait for 1 sec.

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

Could you try delaying the actual method call

removeProductFromCart(id) {
    this.loading = true;
    setTimeout(() => {
        cartHelper.removeFromCart(id, (response) => {
            this.$store.dispatch('removeProductFromCart', {
                cart: response.data,
            })
            this.loading = false
        });
    }, 1000)
over 4 years ago · Santiago Trujillo Denunciar

0

The problem here is that you are only delaying the loading flag, but not the actual action itself. Move your dispatching action inside the setTimout too.

    removeProductFromCart(id) {
        cartHelper.removeFromCart(id, (response) => {
            this.loading = true;
            
            setTimeout(() => {
                this.$store.dispatch('removeProductFromCart', {
                   cart: response.data,
                })
                this.loading = false
            }, 1000);
        });
    }
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda