admin管理员组文章数量:1391955
Trying to get a centered popup to display after a set value of time, something like this:
<script type="text/javascript">
<!--
function popup(url) {
var width = 300;
var height = 200;
var left = (screen.width - width) / 2;
var top = (screen.height - height) / 2;
var params = 'width=' + width + ', height=' + height;
params += ', top=' + top + ', left=' + left;
params += ', directories=no';
params += ', location=no';
params += ', menubar=no';
params += ', resizable=no';
params += ', scrollbars=no';
params += ', status=no';
params += ', toolbar=no';
newwin = window.open(url, 'windowname5', params);
if (window.focus) {
newwin.focus()
}
setTimeout(popup('`test/login.html'), 5000);
return false;
}
//-->
//]]>
</script>
but the popup never displays. If I write it like this:
<script type="text/javascript">
<!--
function popup(url) {
var width = 300;
var height = 200;
var left = (screen.width - width) / 2;
var top = (screen.height - height) / 2;
var params = 'width=' + width + ', height=' + height;
params += ', top=' + top + ', left=' + left;
params += ', directories=no';
params += ', location=no';
params += ', menubar=no';
params += ', resizable=no';
params += ', scrollbars=no';
params += ', status=no';
params += ', toolbar=no';
newwin = window.open(url, 'windowname5', params);
if (window.focus) {
newwin.focus()
}
return false;
}
setTimeout(popup('`test/login.html'), 5000);
//-->
//]]>
</script>
the popup displays immediately, but then I receve an Invalid Argument error. Suggestions?
Trying to get a centered popup to display after a set value of time, something like this:
<script type="text/javascript">
<!--
function popup(url) {
var width = 300;
var height = 200;
var left = (screen.width - width) / 2;
var top = (screen.height - height) / 2;
var params = 'width=' + width + ', height=' + height;
params += ', top=' + top + ', left=' + left;
params += ', directories=no';
params += ', location=no';
params += ', menubar=no';
params += ', resizable=no';
params += ', scrollbars=no';
params += ', status=no';
params += ', toolbar=no';
newwin = window.open(url, 'windowname5', params);
if (window.focus) {
newwin.focus()
}
setTimeout(popup('`test/login.html'), 5000);
return false;
}
//-->
//]]>
</script>
but the popup never displays. If I write it like this:
<script type="text/javascript">
<!--
function popup(url) {
var width = 300;
var height = 200;
var left = (screen.width - width) / 2;
var top = (screen.height - height) / 2;
var params = 'width=' + width + ', height=' + height;
params += ', top=' + top + ', left=' + left;
params += ', directories=no';
params += ', location=no';
params += ', menubar=no';
params += ', resizable=no';
params += ', scrollbars=no';
params += ', status=no';
params += ', toolbar=no';
newwin = window.open(url, 'windowname5', params);
if (window.focus) {
newwin.focus()
}
return false;
}
setTimeout(popup('`test/login.html'), 5000);
//-->
//]]>
</script>
the popup displays immediately, but then I receve an Invalid Argument error. Suggestions?
Share Improve this question asked Jul 23, 2012 at 19:02 sixtwowaifusixtwowaifu 7974 gold badges17 silver badges41 bronze badges 1- 1 setTimeout(function(){popup('`test/login.html')}, 5000); – 001 Commented Jul 23, 2012 at 19:11
2 Answers
Reset to default 4setTimout
takes a function as its first parameter. popup(...)
does not return a function. Try this:
setTimeout(function() {popup('`test/login.html');}, 5000)
In your first example, you placed the setTimeout
within the body of the popup()
function. The body of that function won't be run until the function is called. So, you're right in your second attempt to place the setTimeout
outside of the popup()
body.
The other problem is that you need to pass either a function or code to setTimeout()
:
var timeoutID = window.setTimeout(func, delay, [param1, param2, ...]);
var timeoutID = window.setTimeout(code, delay);
Like so:
setTimeout(function() {
popup('`test/login.html'); // provided that tick-mark is right...
}, 5000);
Whereas your existing method will execute popup()
immediately and provide no function to the timout function:
setTimeout(popup('`test/login.html'), 5000);
本文标签: Delayed Centered Popup Javascript using setTimeoutStack Overflow
版权声明:本文标题:Delayed Centered Popup Javascript using setTimeout - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744734935a2622268.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论