Hello and apologies if this has been asked and answered, but after couple hours each day for the past week searching, I have yet to find a good solution.
Issue: I have a Python/Flask served site where I have a specific button that will allow me to open up every instance of my customers sites, loaded from a database (custdata). The below code in it's original form works fine to open up all of the pages in new tabs. The issue I am having is to help reduce the initial load upon requesting the pages, I would like to have a half second delay between each iteration within the for loop.
Notes: As seen below, I have tried to use the setTimeout option with a delay of 500 milliseconds, but this seems to only delay the opening of the first iteration of the subsequent pages. The loop then opens the rest of the pages without any delay.
For example: If the database has 5 customers for a given server, the page will load, delay for 500ms, then load the 5 customer pages without any delay between them. The desired effect is to delay the opening of the customer pages between each customer page.
<head>
<script>
function myFunction()
{
{% for mydata in custdata %}
window.setTimeout(function(){
window.open("https://customer.mysite.com/launch/{{ custdata.customer_name }}", "_blank");
}, 500);
{% endfor %}
}
</script>
</head>
<body onload="myFunction()">
Customer pages should have loaded. If so, you can close this page. If not, ensure popups are not blocked for this site.
</body>
</html>```
Thanks Paul M for the suggestion. below is the updated code to raise the timeout by 250ms per iteration within the for loop. This appears to have accomplished the end goal I was looking for:
<script>
function myFunction()
{
let delayiteration=0;
{% for mydata in custdata %}
window.setTimeout(function(){
window.open("https://customer.mysite.com/launch/{{ custdata.customer_name }}", "_blank");
}, delayiteration);
delayiteration=delayiteration+250;
{% endfor %}
}
</script>
</head>
<body onload="myFunction()">
Customer pages should have loaded. If so, you can close this page. If not, ensure popups are not blocked for this site.
</body>
</html>