When the original key expires, subsequent keys are generated immediately instead of waiting for new keys to expire. cycling through keys
async function getActivationToken() {
let json = await fetch(`/api/user/activate`, {headers: {"Authorization": token}}).then(response=>response.json());
setActivationText(json);
}
function setActivationText(json) {
let timeleft = json.data.expires - Math.trunc(Date.now()/1000);
let activationtext = document.getElementById("activation-key");
if(timeleft < 1) {
activationtext.innerHTML = `<h1 >Your activation key is: <span >Loading...</span></h1><h1 >This key will expire in <span >Loading...</span></h1>`
return getActivationToken();
}
else {
activationtext.innerHTML = `<h1 >Your activation key is: <span >${json.data.activation}</span></h1><h1 >This key will expire <span >in ${secondsToMinutesAndSeconds(timeleft)}</span></h1>`;
return setInterval(setActivationText, 1000, json);
}
}
getActivationToken();
I tried setting a timeout on the getActivationToken() function, but that just added a second to the wait time after the first key was generated and resulted in the same problem.
async function getActivationToken() {
let json = await fetch(`/api/user/activate`, {headers: {"Authorization": token}}).then(response=>response.json());
setTimeout(setActivationText, 1000, json);
}
Any help would be appreciated
CodePudding user response:
you are creating more and more time interval when timeleft
greater than 1
.
in else statement in setActivationText
, it should be setTimeout
instead of setInterval
.