From 370cc060fbb4965914f6c33babf299cf9fe97731 Mon Sep 17 00:00:00 2001 From: Adam Greenfield Date: Wed, 20 May 2020 12:16:23 -0700 Subject: [PATCH] Prevent a call to update from running before data has been initialized. --- js/scripts.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/js/scripts.js b/js/scripts.js index 51fd89b..cadbe17 100644 --- a/js/scripts.js +++ b/js/scripts.js @@ -36,6 +36,10 @@ const checkRadioByValue = function (radio_array, value) { radio_array.find(radio => radio.value == value).checked = true; }; +const state = { + initialized: false, +}; + const sell_inputs = getSellFields(); const buy_input = $("#buy"); const first_buy_radios = getFirstBuyRadios(); @@ -92,6 +96,9 @@ const initialize = function () { update(); } }); + + console.log('finished initializing'); + state.initialized = true; }; const updateLocalStorage = function (prices, first_buy, previous_pattern) { @@ -364,6 +371,11 @@ const flashMessage = function(message) { }; const update = function () { + if(!state.initialized){ + console.log('update function called before initial data load'); + // calls to update before the previous data has been initialized / loaded will reset the data. + return; + } const sell_prices = getSellPrices(); const buy_price = parseInt(buy_input.val()); const first_buy = getCheckedRadio(first_buy_radios) == 'true';