From b513e3d577b4cc680c76031aed90130f63f9eb47 Mon Sep 17 00:00:00 2001 From: nedu64 Date: Fri, 15 Nov 2024 05:39:12 +0100 Subject: [PATCH] WIP: integrate debounce to qty inputs --- src/web/assets/checkout/dist/js/app.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/web/assets/checkout/dist/js/app.js b/src/web/assets/checkout/dist/js/app.js index a756690..37bf568 100644 --- a/src/web/assets/checkout/dist/js/app.js +++ b/src/web/assets/checkout/dist/js/app.js @@ -81,6 +81,16 @@ const Payment = (props) => { } }; +function debounce(fn, delay) { + let timeout; + return function (...args) { + clearTimeout(timeout); + // We have access to this + console.log(this); + timeout = setTimeout(() => fn.apply(...args), delay); + }; +} + const LineItem = (props) => { return { id: props.lineItemId, @@ -95,14 +105,15 @@ const LineItem = (props) => { showErrorStockMessage: props.showErrorStockMessage, action: '', sending: false, - input() { + input: debounce(() => { + // this is undefined this.qty = this.qty.replace(/\D/g, ''); - setTimeout(() => { - if(this.qty) { - this.updateQty(); - } - }, 700); - }, + + if(this.qty) { + this.updateQty(); + console.log('update quantity'); + } + }, 700), increment() { this.removeMessages(); this.qty++;