I'm trying to open each link from the below list and trying to do some action once the page loads.
The flow should be like,
- open the link
- do the some action
- open the next link from the list
- do the some action
- move to the next link from the list
I have the below code. Instead of opening the first link, it directly jumps to second link and performs the action. I'm not very bright with Js Async.
Much appreciate your help on this!
let lst = ["https://twitter.com/pinsky", "https://twitter.com/adamiqshan"]
lst.forEach( link => {
setTimeout(openLink(link), 5000)
})
function openLink(URL){
window.open(URL, "_self")
}
setTimeout
- the first parameter should be a function, so your code should saysetTimeout(() => openLink(link), 5000)
. But Marc's answer below using Promises is a cleaner way to handle this situation.