Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • prevent countdown timer for being restarted

    • 0
    • 0
    • 0
    • 5
    • 0
    • 0
    • 0
    • 516
    Answer it

    how to prevent countdown timer for being restart when you hit refresh in your browser

    		<div id="quiz-time-left"> </div>
    		<script type="text/javascript">
    			var total_seconds = 60*10 ;
    			var minutes = parseInt(total_seconds/60);
    			var seconds = parseInt(total_seconds%60);
    			function countDownTimer(){
    				if(seconds < 10){
    					seconds= "0"+ seconds ;
    				}if(minutes < 10){
    					minutes= "0"+ minutes ;
    				}
    				
    			document.getElementById("quiz-time-left").innerHTML
    				= "Time Left :"+minutes+"minutes"+seconds+"seconds";
    				if(total_seconds <= 0){
    					setTimeout("document.quiz.submit()",1);
    				}else{
    					total_seconds = total_seconds -1 ;
    					minutes = parseInt(total_seconds/60);
    		            seconds = parseInt(total_seconds%60);
    		            setTimeout("countDownTimer()",1000);
    				}
    			}
    		setTimeout("countDownTimer()",1000);
    	
    			
     function finishpage(){
    alert("unload event detected!");
    document.quiz.submit();
    }
    
    window.onbeforeunload= function() {
    setTimeout('document.quiz.submit()',1);
    }
    
    			
    		</script>

     

 5 Answer(s)

  • According to your code you are initialising with total_seconds = 60*10 and after each refresh total_seconds is again set to constant value.
    You should store your last counter value in localStorage and initialise total_seconds with that updated localStorage value.

    if(localStorage.getItem("total_seconds")){
        var total_seconds = localStorage.getItem("total_seconds");
    } else {
        var total_seconds = 60*10;
    }
    var minutes = parseInt(total_seconds/60);
    var seconds = parseInt(total_seconds%60);
    function countDownTimer(){
        if(seconds < 10){
            seconds= "0"+ seconds ;
        }if(minutes < 10){
            minutes= "0"+ minutes ;
        }
        document.getElementById("quiz-time-left").innerHTML = "Time Left :"+minutes+"minutes"+seconds+"seconds";
        if(total_seconds <= 0){
            setTimeout("document.quiz.submit()",1);
        } else {
            total_seconds = total_seconds -1 ;
            minutes = parseInt(total_seconds/60);
            seconds = parseInt(total_seconds%60);
            localStorage.setItem("total_seconds",total_seconds)
            setTimeout("countDownTimer()",1000);
        }
    }

  • hi can you help me again? first your code is working very well but i had little problem is it that i'm using this code for online test and if you finish up before time is up and start an another test the timer will start from where the other test is submitted
    can i clear the timer or destroy it as we do to the cookie

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: