I am currently using the javascript setTimeout function to continuously loop through the same 4 different websites every 10 seconds. I need an accurate way to show a countdown timer of how many seconds are left before the next website is displayed. Is there a way to subtract the time being used in the setTimeout function already being used to create the timer? Here is the current Javascript that I am using:
<script type="text/javascript">
var pages = [
'webpage 1',
'webpage 2',
'webpage 3',
'webpage 4'
], p = pages.length;
while(--p > -1){
(function(p){
var a = document.createElement('a');
a.href = pages[p];
pages[p] = a;
})(p);
}
function loadIframe() {
var page = pages[(p = ++p % pages.length)], bust = 'bustcache=' + new
Date().getTime();
page = page.search? page.href + '&' + bust : page.href + '?' + bust;
document.getElementById('if_one').src = page;
setTimeout(loadIframe, 10000);
}
loadIframe();
</script>
While you can't see it directly from setTimeout(), it's easy enough to just record the start time and do a little math:
const delay = 10000;
const start = Date.now(); // milliseconds
setTimeout(doSomething, delay);
// when needed
const elapsed = Date.now() - start;
const remaining = delay - elapsed; // divide by 1000 for seconds
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With