Page 1:
function tempWindow(text)
{
var w = window.open('https://example.com/?data='+text, '_blank');
var t = setInterval(() => {
if(w.closed)
{
clearInterval(t);
return true;
}
}, 100);
}
var list = ['text1', 'text2', 'text3'];
for(var i=0;i<list.length;i++)
{
tempWindow(list[i]);//Each loop iteration must wait for each window to close
}
Temporary window:
(function(){
setTimeout(() => {
window.close();
}, 5000);
})();
The code on page 1 should open three windows and send to each of them the strings text1, text2, text3, stored in the variable data.
These windows must not be opened at the same time. For this, setInterval checks every 100 milliseconds if the previous window has already been closed.
Therefore, the second window can only be opened after closing the first one and so on.
I believe that the tempWindow function could return a promise and that using await a solution would be possible, but I don't know how to do that.
use that code. it will wait until window is close
function tempWindow(text) {
return new Promise(resolve => {
var w = window.open('https://example.com/?data=' + text, '_blank');
var t = setInterval(() => {
if (w.closed) {
clearInterval(t);
resolve(true);
}
}, 100);
})
}
var list = ['text1', 'text2', 'text3'];
for (var i = 0; i < list.length; i++) {
await tempWindow(list[i]);//Each loop iteration must wait for each window to close
}
make sure loop code is inside async function like that
async testFunction(){
var list = ['text1', 'text2', 'text3'];
for (var i = 0; i < list.length; i++) {
await tempWindow(list[i]);//Each loop iteration must wait for each window to close
}
}
call function like this testFunction()