I have a timer I use to count how many masks are made in a production environment. I use a variable value that is user input when the page is started – it is called SAMS, basically how many masks 1 person can make in an hour would be what I would input depending on how many people I have working. IE. 2 people may be 18, 1 may be 9 etc. The problem I have with my current build is that I cannot change the SAMS value if I have someone leave for the day. So then what I use that value to calculate is off (I use it to show a GOAL value that increments based on the value).
Here is the relevant code for the processes I’ve described.
var SAMSINPUT = input; console.log(SAMSINPUT); document.getElementById('output').innerHTML = SAMSINPUT; var goal = 0; var output2 = document.getElementById('output2'); setInterval(function doIncrement() { if (clear == false) { goal += 1; output2.innerHTML = goal.toString(); } }, SAMSINPUT * 1000);
I also have START/STOP/RESET buttons on the page for the timer itself. I tried putting the SAMSINPUT input into the stop button, because if I could stop it, put in a new SAMSINPUT – and have the increment adjusted to the new value, that would solve this for me. However, that doesn’t seem to change the actual value that SetInterval references, if I start with a 5, then change it to a 10 that way, it still increments at 5.
Advertisement
Answer
Would this work? Let me know if you have any problems/issues.
var SAMSINPUT = 1; document.getElementById('output').innerHTML = SAMSINPUT; var goal = 0; var output2 = document.getElementById('output2'); function doIncrement() { goal += 1; output2.innerHTML = goal.toString(); } var inter = setInterval(doIncrement, SAMSINPUT * 1000); function changeAmount() { var newVal = document.getElementById('newval').value clearInterval(inter); var a = setInterval(doIncrement, newVal * 1000); }
<div id='output'> </div> <div id='output2'> </div> <input type="number" id="newval"> <button onclick='changeAmount()'>new SAMSINPUT</button>