kishankakadiya kishankakadiya - 3 months ago 42
Javascript Question

when refresh browser page and countdown timer should not continue

View:

<div class="box-body">
<h2><p style="float: right" id="countdown"></p></h2>
</div>

<script>
$time_limit = $("#time_limit").val(); //2016-08-14 00:10:00
var d = new Date($time_limit);
var hours = d.getHours(); //00 hours
var minutes = d.getMinutes(); //10 minutes
var seconds = 60 * minutes; // 600seconds

function secondPassed() {
var minutes = Math.round((seconds - 30) / 60);
console.log(minutes);
var hours = Math.round((minutes) / 60);
var remainingSeconds = seconds % 60;
if (remainingSeconds < 10) {
remainingSeconds = "0" + remainingSeconds;
}
document.getElementById('countdown').innerHTML = hours + ":" + minutes + ":" + remainingSeconds;
if (seconds == 0) {
clearInterval(myVar);
document.getElementById('countdown').innerHTML = "Time Out";
} else {
seconds--;
console.log(seconds);
}

}
var myVar = setInterval(secondPassed, 1000);

</script>


enter image description here

MY Question: When i browser page refresh and countdown timer should not continue, i click next button and browser page refresh and countdown timer should not continue .......................................................

Answer
if (typeof(Storage) == "undefined") 
{
    alert("Your browser does not support web storage");
    //Abort the script;
    //throw new Error("Something went badly wrong!");
}


$time_limit = $("#time_limit").val();
var d = new Date($time_limit);
var hours = d.getHours();                  //00 hours
var minutes = d.getMinutes();              //10 minutes
var seconds = 60 * minutes;
if (localStorage.getItem("seconds") !== null) 
{
    seconds = localStorage.getItem("seconds");
}



function secondPassed() 
{
            var minutes = Math.round((seconds - 30) / 60);
            console.log(minutes);
            var hours = Math.round((minutes) / 60);
            var remainingSeconds = seconds % 60;
            if (remainingSeconds < 10) {
                remainingSeconds = "0" + remainingSeconds;
            }
            document.getElementById('countdown').innerHTML = hours + ":" + minutes + ":" + remainingSeconds;
            if (seconds == 0) {
                clearInterval(myVar);
                document.getElementById('countdown').innerHTML = "Time Out";
            } else {
                seconds--;
                console.log(seconds);
            }
            localStorage.setItem("seconds",seconds);
}

var myVar = setInterval(secondPassed, 1000);