如果弹出窗口关闭,Javascript停止/重置计时器

Javascript stop/reset timer If Popup window is closed

本文关键字:停止 计时器 Javascript 窗口 如果      更新时间:2023-09-26

嘿,伙计们,我是JavaScript的新手,但我想完成一些

我有这个JavaScript定时器脚本,我想打开一个弹出窗口&点击按钮启动计时器,如果我打开的弹出窗口关闭,也可以重置和停止计时器。

这是我的计时器代码:

<script type='text/javascript'>//<![CDATA[ 
window.onload=function(){
var isWaiting = false;
var isRunning = true;
var seconds = 15;
var countdownTimer;
var finalCountdown = false;
function GameTimer() {
    var minutes = Math.round((seconds - 30) / 60);
    var remainingSeconds = seconds % 60;
    if (remainingSeconds < 10) {
        remainingSeconds = "0" + remainingSeconds;
    }
    document.getElementById('waiting_time').innerHTML = "<i class='fa fa-spin fa-spinner'></i> " + minutes + ":" + remainingSeconds;
    if (seconds == 0) {
                    document.getElementById('waiting_time').innerHTML = "" ;
                    document.getElementById('step').innerHTML = "<button class='button' onclick='window.location.reload();return false;'><font color='#8d9d29'><i class='fa fa-arrow-circle-right'></i> Next step</font></button>" ;
        if (finalCountdown) {
            clearInterval(countdownTimer);
        } else {
            finalCountdown = true;
        }
    } else {
        isWaiting = true;
        seconds--;
        if (seconds == 0) {
            seconds=seconds;}     

    }
}
countdownTimer = setInterval(GameTimer, 1000);
}//]]>  
</script>

这会检查弹出窗口是否已关闭

var child = window.open('http://google.com/','','toolbar=0,status=0,width=926,height=536');
    var timer = setInterval(checkChild, 500);
    function checkChild() {
        if (child.closed) {
            /// something 
            clearInterval(timer);
        }
     }

如何合并这两个片段?

我真的不确定这是否是你想要的。我不知道我是否理解你需要什么。

我只是稍微重新构造了一下代码。主要的是,我将变量放入一个名为GameObject 的全局对象中

<script type='text/javascript'>//<![CDATA[ 
    var GameObject = {
        "isWaiting" : false,
        "isRunning" : true,
        "seconds" : 15,
        "finalCountdown" : false,
        "child" : null,
        "countdownTimer" : null,
        "gametimer" : null
    };
    function onButtonClick() {
        //Open the window
        GameObject.child = window.open('http://google.com/','','toolbar=0,status=0,width=926,height=536');
        //Start the window open check interval
        GameObject.gametimer = setInterval(function() {
            if (GameObject.child.closed) {
                /// something 
                clearInterval(GameObject.gametimer);
                clearInterval(GameObject.countdownTimer);
            }
        }, 500);
        //Start the game timer
        GameObject.countdownTimer = setInterval(GameTimer, 1000);
    }
    function GameTimer() {
        var minutes = Math.round((GameObject.seconds - 30) / 60);
        var remainingSeconds = GameObject.seconds % 60;
        if (remainingSeconds < 10) {
            remainingSeconds = "0" + remainingSeconds;
        }
        document.getElementById('waiting_time').innerHTML = "<i class='fa fa-spin fa-spinner'></i> " + minutes + ":" + remainingSeconds;
        if (GameObject.seconds == 0) {
            document.getElementById('waiting_time').innerHTML = "" ;
            document.getElementById('step').innerHTML = "<button class='button' onclick='window.location.reload();return false;'><font color='#8d9d29'><i class='fa fa-arrow-circle-right'></i> Next step</font></button>" ;
            if (GameObject.finalCountdown) {
                clearInterval(GameObject.countdownTimer);
            } else {
                GameObject.finalCountdown = true;
            }
        } else {
            GameObject.isWaiting = true;
            GameObject.seconds--;
            if (GameObject.seconds == 0) {
                GameObject.seconds = GameObject.seconds;
            }
        }
    }
//]]>
</script>