I try to create a JavaScript function that, when user clicks on a button, opens a new tab after a small timeframe then wait one second and close it.
Currently, my code looks like this:
var btn = document.getElementById("myLink");
btn.onclick = function() {
setTimeout(function() {
var win = window.open(
"http://www.stackoverflow.com",
"The new Link")
},500);
setTimeout(function(){
win.close();
if (win.closed) {
clearInterval(timer);
winClosed();
alert("'The new Link' window closed !");
}
}, 2500);
}
But the second setTimeout function is not executed. When I remove one of the two setTimeout function, the other one works fine, but I need to have the two running.
Edit: changed timeout values to take into account @lk77 comment.