gpt4 book ai didi

javascript - setTimeout() 在 setTimeout 激活之前获取窗口以显示其他脚本函数

转载 作者:行者123 更新时间:2023-11-30 13:27:17 26 4
gpt4 key购买 nike

我确信这是一个被问过很多次的通用问题,但找不到解决方案。

我有 javascript 使用 setTimeout() 函数来关闭我在设定时间后创建的弹出窗口。

问题:如果我在与创建弹出窗口的脚本相同的脚本中调用 setTimeout() 函数,则弹出窗口不会显示窗口的内容,而是整个功能就像单个脚本一样,然后窗口关闭。就像您需要以某种方式中断脚本以使其在执行 setTimeout 之前解析脚本的每个部分。

有人知道原因或有解决方法吗?

<script>
var w;
function closeWindow(){ setTimeout(w.close();, 10000);}

function createWindow(){
//create the popup window.

w=window.open("","",'width=200,height=100');

// put something into the popup window
try{w.document.write('<html><head></head><body><p>the w window</p></body>
<html>')}catch(err){
//handle error here
}

closeWindow();

//closes the createWindow() function
}
</script>

最佳答案

我试过你的脚本。以下内容已经过测试并正常运行:

var w;
function closeWindow(){ setTimeout("w.close();", 10000);}

function createWindow(){
//create the popup window.

w=window.open("","",'width=200,height=100');

// put something into the popup window
try{w.document.write('<html><head></head><body><p>the w window</p></body> <html>')}catch(err){
//handle error here
}
closeWindow();
//closes the createWindow() function
}

如果我没记错的话,setTimeout 函数的代码参数周围需要引号,否则您会将代码的返回值作为 setTimeout 的参数传递,而不是它需要运行的代码。

希望这对您有所帮助。

关于javascript - setTimeout() 在 setTimeout 激活之前获取窗口以显示其他脚本函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8035422/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com