0

have a goog day , i am facing some problem in the countdown timer

<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>

now my problem is when i click refresh, the form which i do not show here will will be submitted without alert and when the time finish there is a alert box pop out, now actually i hope can get the confirm box with yes,no to let the user choose,but cant make it,so can somebody help me? thanks

<script type="text/javascript">

                            window.onbeforeunload= function() {
                            var isOk = confirm("Are you sure?");
                            if(isOk) 
                            {
                            setTimeout('document.myForm.submit()',1);
                            }
                            }

                            </script>

but when i click F5 for refresh ,the box do not show

boon
  • 11
  • 5

1 Answers1

0

Just a plain simple confirm box?

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

Then you can use isOk to see if you should submit your form:

if(isOk) 
  document.myForm.submit()
JAM
  • 6,045
  • 1
  • 32
  • 48
  • may i know where should put this code? window.onbeforeunload= function() { var isOk = confirm("Are you sure?"); if(isOk) { setTimeout('document.myForm.submit()',1); } is it look like this?but when i click F5 for refresh ,the box do not show out } – boon May 13 '13 at 23:27
  • Ah, I misunderstood you question. I'm not sure if you can do what you're asking. You may want to check out this post: http://stackoverflow.com/questions/1335727/onbeforeunload-confirmation-screen-custumization – JAM May 13 '13 at 23:30