如何在倒计时计时器刷新时显示确认框

how to show confirm box when refresh in countdown timer

本文关键字:显示 确认 刷新 计时器 倒计时      更新时间:2023-09-26

祝你今天过得愉快,我的倒数计时器出了点问题

<div style="font-weight: bold; float:right;" id="quiz-time-left"></div>
<script type="text/javascript">
window.onbeforeunload= function() {
setTimeout('document.myForm.submit()',1);
}
</script>
<script type="text/javascript">
var max_time = 10;
var c_seconds  = 0;
var total_seconds =60*max_time;
max_time = parseInt(total_seconds/60);
c_seconds = parseInt(total_seconds%60);
document.getElementById("quiz-time-left").innerHTML='Time Left: ' + max_time + ' minutes ' + c_seconds + ' seconds';
function init(){
document.getElementById("quiz-time-left").innerHTML='Time Left: ' + max_time + ' minutes ' + c_seconds + ' seconds';
setTimeout("CheckTime()",999);
}
function CheckTime(){
document.getElementById("quiz-time-left").innerHTML='Time Left: ' + max_time + ' minutes ' + c_seconds + ' seconds' ;
    if(total_seconds <=0){
    alert("Time Up!");                      setTimeout('document.myForm.submit()',1);
} else
{
total_seconds = total_seconds -1;
max_time = parseInt(total_seconds/60);
c_seconds = parseInt(total_seconds%60);
setTimeout("CheckTime()",999);
}
}
init();
</script>

现在我的问题是,当我点击刷新时,我没有在这里显示的表单将会在没有警告的情况下提交,当时间结束时有一个警告框弹出,现在实际上我希望可以得到确认框yes,no让用户选择,但不能做到,所以有人能帮助我吗?由于

<script type="text/javascript">
                            window.onbeforeunload= function() {
                            var isOk = confirm("Are you sure?");
                            if(isOk) 
                            {
                            setTimeout('document.myForm.submit()',1);
                            }
                            }
                            </script>

,但当我按F5刷新,框不显示

只是一个简单的确认框?

var isOk = confirm("Are you sure?")

然后你可以使用isOk查看你是否应该提交你的表单:

if(isOk) 
  document.myForm.submit()