Goal: Delay execution of function by at least 1 second.
The problem: Instead of a minimum 1 second delay the do / while
loops forever.
The steps:
- After 1 second
timer
is set totimerLength
. - If the
fetch
request takes more than 1 second no further delay is added. - If the
fetch
request takes less than 1 second further execution will be delayed by up to 1 second bydo / while
loop.
let timer = 0;
const timerLength = 1000;
setTimeout( () => {
timer = timerLength;
}, timerLength);
// fetch request and other code here...
do { } while(timer !== timerLength); // no code inside do while
// Code after this point is expected to run after a minimum delay of 1 second after setTimeout.