There is a need to write a simple counter (as goods in the basket online store + - the value of the input). I always did something like this and there were no problems, everything worked (I simplified the essence):
var count = 1; input.value = count; next.addEventListener('click', function(){ if(count < 4) { count++; input.value = count; } }); prev.addEventListener('click', function() { if (count > 1) { count--; input.value = count; } }); It counts from 1 to 4.
But now you need to insert the counter into the modal window, which will be called by clicking on different elements. The counter will be initialized when the window appears (put the counter in the function). At first glance, everything works fine, everything switches, when you close the window and call another window, the field value is set to 1 (as indicated in the 1st line), BUT if you press the back button immediately after the counter appears the second ++ time (if the previous window was is closed on a number more than 1), then it starts counting back from the value on which the past window was closed, ignoring if(count>1) , as if count keeps the value from the previous window, although in theory I reassigned it to 1 on the first line.
I would like to understand what is the reason, what principle does it work and how best to solve this problem?