0

尝试在设定的时间值后显示居中的弹出窗口,如下所示:

<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>

弹出窗口立即显示,但随后我收到一个 Invalid Argument 错误。建议?

4

2 回答 2

4

setTimout将函数作为其第一个参数。 popup(...)不返回函数。试试这个:

setTimeout(function() {popup('`test/login.html');}, 5000)
于 2012-07-23T19:10:59.300 回答
2

在您的第一个示例中,您将 放置setTimeout在函数的主体内popup()。在调用该函数之前,该函数的主体不会运行。所以,你在第二次尝试放置身体setTimeout外部时是对的popup()

另一个问题是您需要将函数或代码传递给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);
于 2012-07-23T19:17:02.777 回答