From ce4c495052fb9e02de4d5b6dfb39a106dc2521d1 Mon Sep 17 00:00:00 2001 From: ekzyis Date: Sat, 9 Sep 2023 22:52:50 +0200 Subject: [PATCH] Add debounce logic --- public/bmarket.js | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/public/bmarket.js b/public/bmarket.js index 720675c..b825ae4 100644 --- a/public/bmarket.js +++ b/public/bmarket.js @@ -80,6 +80,21 @@ yesSellBtn.onclick = showSellForm noBuyBtn.onclick = showBuyForm noSellBtn.onclick = showSellForm +function debounce(ms) { + let debounceTimeout = null + return function (fn, ...args) { + return function (e) { + if (debounceTimeout) { + clearTimeout(debounceTimeout) + } + debounceTimeout = setTimeout(() => { + fn(...args)(e) + debounceTimeout = null + }, ms) + } + } +} + function updatePrice(marketId, shareId) { return async function (e) { const quantity = parseInt(e.target.value, 10) @@ -104,5 +119,5 @@ function updatePrice(marketId, shareId) { yesCostDisplay.value = parseFloat(Math.abs(rBody.cost)).toFixed(3) } } -yesQuantityInput.onchange = updatePrice(marketId, yesShareId) -noQuantityInput.onchange = updatePrice(marketId, noShareId) +yesQuantityInput.oninput = debounce(250)(updatePrice, marketId, yesShareId) +noQuantityInput.onchange = debounce(250)(updatePrice, marketId, noShareId)