Javascript: using setTimeout with window.close() not work after using setTimeout with not working
settimeout javascript not working
javascript close window after 5 seconds
let timeoutid settimeout function console log yes 3000 cleartimeout timeoutid javascript
window close not working
javascript popup window onclick new tab

I have two setTimeout after another. One to open a webpage in the window and another to close the window after a predetermine amount of time. However, after using setTimeout to open a webpage, the next setTimeout that close the window does not work. The setTimeout with windows.close by itself works fine.

I and trying to opening a window with a message; then open a website in the window after a predetermine amount of time, and then close the window after a couple of more seconds has passed. Here is my function to do this:

function showNews() {
    news ="", "NewsWindow", "width=900, height=700");
    news.document.write("<p>The 'NewsWindow' will only appearing for 3 seconds.</p>");
    tmot = setTimeout(function(){'','_self')}, 2000);
    tmot = setTimeout(function(){news.window.close()}, 5000);

I tried running on both Chrome and IE and both browser would only execute one of the setTimeout and not the other. Any advice and help would be greatly appreciated!

You'll better reopen it using parent window with the same parameters :

function showNews() {
    news ="", "NewsWindow", "width=900, height=700");
    news.document.write("<p>The 'NewsWindow' will only appearing for 3 seconds.</p>");
    tmot = setTimeout(function(){'', "NewsWindow")},2000);
    tmot = setTimeout(function(){news.close()}, 5000);

Not try on IE, i don't have it.

Window setTimeout() Method, The clearTimeout() method clears a timer set with the setTimeout() method. myVar = setTimeout("javascript function", milliseconds);. Then, if the function has not already been executed, you will be able to stop the execution by The numbers in the table specify the first browser version that fully supports the method. The setTimeout() method calls a function or evaluates an expression after a specified number of milliseconds. Tip: 1000 ms = 1 second. Tip: The function is only executed once. If you need to repeat execution, use the setInterval() method. Tip: Use the clearTimeout() method to prevent the function from running.

I got a solution after a whole day trying the same thing. My occured in some jQuery success result and I thougt first it was because I could not propagate the myWindow var from "success" result to main DOM.

In fact it's only that javascript does not accept to manipulate any window once a new content has been loaded (I mean URL). But it can be hacked still, since it seems javascript ONLY refuse manipulation if there is a NEW content. So if you reload your window with exactly the same content, then it suddendly becomes manipulable.

So somewhere in my code (even in a function) I open the window (wherever it is), with no need to set it in var.'myURL','WindowName', "width=200, height=200");

Then wen I decide to close the window (wether it is a SetTimeOut or any trigger), I just have first to reload the window with exactly the same URL and same name , (and this time with a var set). And now I can magically close it.

var windo ='myURL','WindowName', "width=200, height=200");

Don't ask me why it works. It does.

(I guess it's because it's not a new window not new content for javascript, so rules are respected.)

Window clearTimeout() Method, Learn how the JavaScript function setTimeout() works, how it can be used with If you don't see the popup open, please visit CodePen and run the demo there. For the purposes of this tutorial, I'll omit window , but ultimately, which This won' t work when using an arrow function in the introduce method,� If you are trying to close the popup using some code within the popup then follow Joe's directions, window.close() should be Ok. I was assuming you were trying to close the popup from the main window, you mention two onclicks, the one that opens the pop up is assumably within the main page, where is the other ?

I tested this in JSFiddle on Chrome.

The timeout is, in fact, firing. The problem is that certain browsers do not allow you to close a window using javascript.There are bugs that prevent it - specifically that the browser won't let code from one website close a window that is opened to a different website. See the comment below for a better explanation.

When I tried this in Chrome the setTimeout method fired, but the window did not close. I tested it by calling the 'alert' method rather than the news.window.close() method.

JavaScript setTimeout() Function Examples, So, popups is not something we use everyday. What if the popup opens from onclick , but after setTimeout ? That's a bit tricky. If we decrease the delay, the popup works in Firefox too: To close a window: win.close() . Only if you open a new window using will the new window be able to close using code as I have mentioned above. This works perfectly for me :) Note : Never use href to open the page in a new tab. Window.close() does not work with "href" . Use instead.

Some browsers forbid you from closing the window once it loads a website.

The following works:


<button onclick="showNews()">Show News</button>


var myWindow;

function showNews()
    myWindow ="","myWindow","width=200,height=100");
    myWindow.document.write("<p>This is 'myWindow'</p>");
    //tmot = setTimeout(function(){'','_self')}, 2000);
    tmot = setTimeout(function(){myWindow.close();}, 5000);



View demo

But if we uncomment the line where we load, it doesn't work anymore:

View demo

What to do?

You could try using a hack, something like this:


<button type="button" id="show-news">Show News</button>


$("#show-news").click(function() {

    myWindow ='','myWindow', "width=200, height=200");
    setTimeout(function() {"", "_self");
    }, 2000);

    setTimeout(function() {'', '_self', ''); 
    }, 5000);


Popups and window methods, open after 3 seconds setTimeout ( ( ) => window . open ( '' ) , 3000 ) ; If we decrease the delay, the popup works in Firefox too: A JavaScript in it may use window.opener to access the window that opened it. but window.close() is ignored by most browsers if window is not created with . After the call to setTimeout() the script continues normally, with the timer running in the background. In the above simple example we embedded the entire code for our JavaScript alert box in the setTimeout() call. More usually, you’d call a function instead.

Popups and window methods, Actual results: setTimeout does not works with delay Expected results: it should In this ticket, it seems that there are some limits on eval calling but I am not using it. (b) While alert() by myfunc() at sub window is kept open, and while alert() by because composition window is not cached and actually closed from Tb 17)� Then I can close this window, using. window.close(); But to close the window which is not opened by, just do one thing. open any other URL or a blank page in the same tab. Then close this newly opened tab (because it is opened by code:- <script>"", "_self"); window.close(); </script>

814651, Passing a string instead of a function to setTimeout() has the same associated problems as using eval. // Recommended window.setTimeout(� window.close(); Examples Closing a window opened with This example shows a method which opens a window and a second one which closes the window; this demonstrates how to use Window.close() to close a window opened by calling

WindowOrWorkerGlobalScope.setTimeout(), The window.location object can be used to get information on the the function until clearInterval() is called, or the window is closed. To execute a function only once, after a specified number of milliseconds, use the setTimeout() method The setTimeout() method sets a timer in milliseconds, then calls a� The difference is that Firefox treats a timeout of 2000ms or less are acceptable, but after it – removes the “trust”, assuming that now it’s “outside of the user action”. So the first one is blocked, and the second one is not. The syntax to open a popup is:, name, params): url An URL to load into the new

  • I tried this in Firefox (27) and IE (11), and it works fine.
  • Note that you lose your reference to the first timeout when you set the return of the second to tmot.
  • I tried this in chrome here: And it did not work. Once the window opens a new site the original window can no longer control it, at least on Chrome.
  • Thank you! That was the trick! (removing "news." from the setTimeout lines.) Now to figure out why that is the case...
  • Just figured out why. Thank you!
  • I don't think it's a bug. Once the window is loaded with content from a different domain, the browser probably considers it to be off limits.
  • You're right - I shouldn't have said 'bug'. Sometimes I use the term 'but' when I really mean "the code didn't do what I wanted it to do, but rather what it was designed to do". I'm editing the answer.