简体   繁体   English

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

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

I can't figure out why the window won't close after 86 seconds. 我不知道为什么86秒后窗口无法关闭。

Here's the code: 这是代码:

//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(); 

One problem is that the code is not passing a function to setTimeout , but just a string read via prompt . 一个问题是代码没有将函数传递给setTimeout ,而只是通过prompt读取一个字符串。 (While setTimeout will accept a string, for legacy reasons, it must be valid JavaScript code to make any sense.) (虽然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); 
} 

Compare with: 与之比较:

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);
} 

(Note that I've also made popupWin a var so that it is caught in the closure - thus all the opened windows should be closed after their respective timeout, not just the last one.) (请注意,我也做了一个popupWin var ,使其陷入封闭-因此,所有打开的窗口应该各自超时,而不仅仅是最后一个后关闭)

No where in that code does window.close() get called. 在该代码中,没有位置window.close()被调用。

setTimeout(url, 86000) just schedules the execution of the function url() in about 24 hours. setTimeout(url, 86000)仅在大约24小时内调度函数url()的执行。

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

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