繁体   English   中英

为什么一段时间后我的窗户不会关闭? (JavaScript)

[英]Why wont my window close after a period of time? (Javascript)

我不知道为什么86秒后窗口无法关闭。

这是代码:

//Functions 
function URL() {
    return prompt("Enter the URL."); 
} 

function openNewWindow() { 
    var url = URL(); 
    popupWin = window.open(url, 'open_window', 'menubar, toolbar, location, directories, status, scrollbars, resizable,dependent, width=640, height=480, left=0, top=0');
    setTimeout(url,86000); 
} 

//Main
var url1 = openNewWindow(); 

一个问题是代码没有将函数传递给setTimeout ,而只是通过prompt读取一个字符串。 (虽然setTimeout会接受字符串,但出于传统原因,它必须是有效的JavaScript代码才能有意义。)

function URL() {
    return prompt("Enter the URL."); 
} 

function openNewWindow() { 
    var url = URL();   // url is the RETURN VALUE of calling the URL function ..
    popupWin = window.open(url, 'open_window', 'menubar, toolbar, location, directories, status, scrollbars, resizable,dependent, width=640, height=480, left=0, top=0');
    // .. which is a string (representing a URL), not a function
    setTimeout(url,86000); 
} 

与之比较:

function openNewWindow() { 
    var url = URL();   // url is the RETURN VALUE of calling the URL function ..
    var popupWin = window.open(url, 'open_window', 'menubar, toolbar, location, directories, status, scrollbars, resizable,dependent, width=640, height=480, left=0, top=0');
    // .. but we pass a callback function :)
    setTimeout(function () {
        alert("done");
        // And assuming the browser allows this ..
        popupWin.close();
    }, 4 * 1000);
} 

(请注意,我也做了一个popupWin var ,使其陷入封闭-因此,所有打开的窗口应该各自超时,而不仅仅是最后一个后关闭)

在该代码中,没有位置window.close()被调用。

setTimeout(url, 86000)仅在大约24小时内调度函数url()的执行。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM