yes I'm new, and don't worry about making me sound dumb. :) This is a web app written in Google Script and it's taking info from Google Sheets.
I adopted a code from a comment here: Javascript timer to use multiple times in a page . Current code is below. I'm running several different timers thru this whole thing and they all go down by 1 second every second - great. My question is because the value on the google sheet that the first code grabs, is changing every few minutes. I WANT it to be counting down from the most recent data, and make the "old" counter STOP. From reading several related threads I think I need another clearInterval somewhere but I can't figure out where to put it.
Google Apps Script that grabs Google sheets values and puts them into a variable. the "getdata" values are dates in the future, and then "data" is how many seconds until that date:
function getfivecoins(){
var livesheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Live");
var getdata = livesheet.getRange("D42").getValue().getTime();
var now = new Date().getTime();
var data = (getdata-now)/1000; // how many seconds in the future this date is
return data;
}
function gettencoins(){
var livesheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Live");
var getdata = livesheet.getRange("D43").getValue().getTime();
var now = new Date().getTime();
var data = (getdata-now)/1000; // how many seconds in the future this date is
return data;
}
then in html page:
<span id="timer1"></span>
<br>
<span id="timer2"></span>
<br>
lower down inside of script tags:
// every 10 seconds, gets new values from google sheets. Countdown should continue as normal (with some milliseconds difference, whatever) if values are the same, but trying to get countdown to start again if the value has changed. If its easier, clearing all the countdowns and then starting them again should have the same result.
document.addEventListener('DOMContentLoaded', function(){
setInterval(function(){
google.script.run.withSuccessHandler(generatefivecoins).getfivecoins();
google.script.run.withSuccessHandler(generatetencoins).gettencoins();
}, 10000);
});
const clearStack = []; //Added
function generatefivecoins(result) {
clearStack.forEach(clearInterval); //Added
var timer = document.getElementById('timer1');
var t = new timerObject();
clearStack.push(t.startTimer(result, timer)); //Modified
}
function generatetencoins(result){
clearStack.forEach(clearInterval); //Added
var timer = document.getElementById("timer2");
var t = new timerObject();
clearStack.push(t.startTimer(result, timer)); //Modified
}
var timerObject = function(){
return this;
};
timerObject.prototype.startTimer = function(duration, display) {
var me = this,
timer = duration,
STYLETEST, hours, minutes, seconds, ENDSTYLETEST;
var intervalLoop = setInterval(() => { // modified this line
//fancy colours and formatting stuff goes here
display.innerHTML = STYLETEST + " " + hours + ":" + minutes + ":" + seconds + ENDSTYLETEST;
if (--timer < 0) {
clearInterval(intervalLoop);
}
}, 1000);
return intervalLoop; //Added
};