I am making a script in javascript that from a webpage opens a list of other webpages. I'm able to open all the pages but then I need to close these webpages after some times but appear that setTimeout does not work. Here is the code
function OpenAllLinks() {
const array = Array.from(mySet1); //here I get an array of urls as string
let opened_pages = [];
let openWindow = null;
for(i=0; i<array.length; i++){
openWindow = window.open(array[i], '_blank');
opened_pages.push(openWindow);
}
let win = null;
for(i=0; i<opened_pages.length; i++){
win = opened_pages[i];
console.log(win);
try {
setTimeout(function(){win.close()},2000); // after two seconds the page are still open
} catch (error) {
console.log(error);
}
}
}
So how can I close a list of webpages after some time?