一段时间后window.focus()不起作用

时间:2019-06-14 10:15:37

标签: javascript html dom settimeout

我一直在尝试制作一个计时器,其中会弹出一个带有时钟的新小方形窗口来设置计时器(我们称其为myWin)

我已经从window.open()函数中保存了myWin,并且myWin.focus()setTimeout();的5秒钟后不起作用

我尝试使用window.opener,为此我将计时器代码保留在打开的窗口中。根本没用。

//Code for timer

function btn1Click() {
    myWin = window.open('small.html', "", "width=100, height=100"); //small.html contains the dummy clock in it

    setTimerValue(5, myWin); // 5 seconds is the hardcoded time, it can be any positive integer
    console.log("Opened");
}

function setTimerValue(mytime) {
    console.log(myWin);
    let interval = setInterval(function() {
        console.log(mytime);
        mytime--;

        if (mytime == 0) {
            console.log("I am inevitable...")
            clearInterval(interval);
            myWin.focus(); //doesn't work if the value of time is more than 5 seconds

            console.log("Done");
        }
    }, 1000);

}

setTimeout函数完成工作之后,包含small.html的窗口应该成为焦点

0 个答案:

没有答案