Html 如何在特定时间段内自动打开和关闭弹出窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16807790/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to open and close popups automatically with certain time period
提问by Prateek
I am making a web page, in which I have to open a popup window and it should remain open for a time period of 8 seconds(8000 ms).
我正在制作一个网页,我必须在其中打开一个弹出窗口,它应该保持打开状态 8 秒(8000 毫秒)。
After this time period that popup should be closed. Then again after 4 seconds I need to open same popup window for another 8 seconds.
在这段时间之后,应该关闭弹出窗口。然后在 4 秒后再次打开相同的弹出窗口 8 秒。
I want to put some delay(4 seconds) before a popup open and closes automatically, and the popup window must be remain opened for 8 seconds
我想在弹出窗口自动打开和关闭之前放置一些延迟(4 秒),并且弹出窗口必须保持打开状态 8 秒
Here is my code:
这是我的代码:
<html>
<head>
<script>
function call()
{
popup = window.open('http://www.google.co.in');
setInterval(function() {wait();},4000);
}
function caller()
{
setInterval(function() {call();},5000);
}
function wait()
{
popup.close();
}
</script>
</head>
<body onload="caller();">
</body>
</html>
I am familiar with java script functions like setInterval()
and setTimeout()
but I've found none of them useful in this case.
I've also allowed my browser to open Popups, but this script opens a popup window and closes it as fast as time passes.
Please help me to find out the glitch in my code.
我熟悉 java 脚本函数,例如setInterval()
和setTimeout()
但我发现在这种情况下它们都没有用。我还允许我的浏览器打开弹出窗口,但是这个脚本会随着时间的推移打开一个弹出窗口并关闭它。请帮助我找出代码中的故障。
Thank you.
谢谢你。
采纳答案by ?ēēpak
Your code is looking good in formatting but try to put some efforts on your code.
Try to use it as given below, Here is your whole code, I've tried it on my system, and it is working.
您的代码在格式上看起来不错,但请尝试对您的代码进行一些努力。
尝试按照下面给出的方式使用它,这是您的整个代码,我已经在我的系统上尝试过,并且可以正常工作。
<html>
<head>
<script>
function call()
{
popup = window.open('http://www.google.co.in');
setTimeout(wait, 8000);
}
function caller()
{
setInterval(call, 12000);
}
function wait()
{
popup.close();
}
</script>
</head>
<body onload="caller();">
</body>
</html>
回答by Teemu
Try these:
试试这些:
function call() {
popup = window.open('http://www.google.co.in');
setTimeout(wait, 8000); // closes the pop-up after 8 secs delay
}
function caller(){
setInterval(call, 12000); // opens a pop-up on every 12th second
}
Your current call()
creates a new interval every time it's executed.
call()
每次执行时,您的电流都会创建一个新的间隔。
回答by Luke
Hi you can close the popup window after a particular interval using this below code
嗨,您可以使用以下代码在特定时间间隔后关闭弹出窗口
<button class="btn btn-success" type="submit" id="spk_button" onclick="javascript:setTimeout(function(){window.close()},3000);">Save</button>
回答by David Barker
You are not assigning your intervals to any variables, without having a reference to these intervals they will continue running indefinitely with no way of clearing them. A method of doing what you want would probably be better with setTimeout as the periods between opening / closing and then re-opening are all different, also you have no problems with constantly running intervals. The below code creates a loop that will run indefinitely until you want it to stop.
您没有将您的间隔分配给任何变量,如果没有对这些间隔的引用,它们将无限期地继续运行而无法清除它们。使用 setTimeout 做您想做的事情的方法可能会更好,因为打开/关闭和重新打开之间的时间段都不同,而且您在不断运行间隔时也没有问题。下面的代码创建了一个循环,该循环将无限期运行,直到您希望它停止为止。
var popup = {
open : function()
{
// Open popup
this.popupWindow = window.open('http://www.google.co.in');
// Return a timeout function
return setTimeout(function() {
// run close function on completion of timeout
popup.close();
}, 8000);
},
close : function()
{
this.popupWindow.close();
// Assign timeout to local variable
this.timeout = this.loop();
},
loop : function()
{
var self = this;
// On first run open popup immediately, or wait 4 seconds and restart
// the loop
if (self.active === true)
{
return setTimeout(function() {
self.timeout = self.open();
}, 4000);
}
else
{
self.active = true;
self.timeout = self.open();
}
},
clearLoop : function()
{
clearTimeout(this.timeout);
this.active = false;
}
};
popup.loop();
to stop the loop at anytime you can call
在您可以调用的任何时间停止循环
popup.clearLoop();
回答by Michal Politzer
You can write it as a "scenario":
你可以把它写成一个“场景”:
function next(state) {
switch(state) {
case 0:
showPopup();
setTimeout("next(1);",8000);
break;
case 1:
hidePopup();
setTimeout("next(2);",4000);
break;
case 2:
showPopup();
setTimeout("next(3);",8000);
break;
...etc...
}
}
/* this you have to call at start */
function init() {
next(0);
}
If you need in some step to repeat cycle, you can of course use next(0).
如果您需要在某个步骤中重复循环,您当然可以使用 next(0)。
And of course there have to be functions showPopup and hidePopup
当然,必须有函数 showPopup 和 hidePopup