尝试在设定的时间值后显示居中的弹出窗口,如下所示:
<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>
但弹出窗口从不显示。如果我这样写:
<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>
弹出窗口立即显示,但随后我收到无效参数错误。建议?
答案 0 :(得分:3)
setTimout
将函数作为其第一个参数。 popup(...)
不返回函数。试试这个:
setTimeout(function() {popup('`test/login.html');}, 5000)
答案 1 :(得分:2)
在第一个示例中,您将setTimeout
放在popup()
函数的正文中。在调用函数之前,该函数的主体不会运行。因此,您在第二次尝试将setTimeout
置于popup()
身体之外时是正确的。
另一个问题是您需要将a function or code传递给setTimeout()
:
var timeoutID = window.setTimeout(func, delay, [param1, param2, ...]);
var timeoutID = window.setTimeout(code, delay);
像这样:
setTimeout(function() {
popup('`test/login.html'); // provided that tick-mark is right...
}, 5000);
虽然现有方法会立即执行popup()
,但不会为timout函数提供任何功能:
setTimeout(popup('`test/login.html'), 5000);