Javascript:在 window.open() 中使用 setTimeout 后,将 setTimeout 与 window.close() 一起使用不起作用 [英] Javascript: using setTimeout with window.close() not work after using setTimeout with window.open()

查看:170
本文介绍了Javascript:在 window.open() 中使用 setTimeout 后,将 setTimeout 与 window.close() 一起使用不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 setTimeout 一个接一个.一个在窗口中打开网页,另一个在预定时间后关闭窗口.但是,使用setTimeout打开网页后,下一次关闭窗口的setTimeout不起作用.带有 windows.close 的 setTimeout 本身工作正常.

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 = window.open("", "NewsWindow", "width=900, height=700");
    news.document.write("<p>The 'NewsWindow' will only appearing for 3 seconds.</p>");
    tmot = setTimeout(function(){news.window.open('http://www.yahoo.com','_self')}, 2000);
    tmot = setTimeout(function(){news.window.close()}, 5000);
}

我尝试在 Chrome 和 IE 上运行,但两个浏览器都只会执行 setTimeout 之一,而不会执行另一个.任何建议和帮助将不胜感激!

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 = window.open("", "NewsWindow", "width=900, height=700");
    news.document.write("<p>The 'NewsWindow' will only appearing for 3 seconds.</p>");
    tmot = setTimeout(function(){window.open('http://www.yahoo.com', "NewsWindow")},2000);
    tmot = setTimeout(function(){news.close()}, 5000);
}

不要尝试 IE,我没有.

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

这篇关于Javascript:在 window.open() 中使用 setTimeout 后,将 setTimeout 与 window.close() 一起使用不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆