I'm using count down timer by this code. If the page refresh or reload the count should not be reset for that I'm using this from localstorage. If there any alternate solution for this means Please suggest me.
var hms = $(".div__time .total_time").text();
var a = hms.split(':');
var hrs_min_sec = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);
var time_hrs_min_sec = hrs_min_sec;
if (localStorage.getItem("counter")) {
if (localStorage.getItem("counter") <= 0) {
var value = time_hrs_min_sec;
} else {
var value = localStorage.getItem("counter");
}
} else {
var value = time_hrs_min_sec;
}
document.getElementById('overall_time').innerHTML = value;
var counter = function() {
if (value <= 0) {
localStorage.setItem("counter", time_hrs_min_sec);
} else {
value = parseInt(value) - 1;
console.log(value);
localStorage.setItem("counter", value);
}
document.getElementById('overall_time').innerHTML = value;
if (value == 0) {
// var redirect_url = "<?php echo site_url('home'); ?>";
// window.location.href = redirect_url;
}
var hours = Math.floor(value / 3600);
var minutes = Math.floor(value % 3600 / 60);
var seconds = Math.floor(value % 3600 % 60);
var red_time = hours + ' : ' + minutes + ' : ' + seconds;
document.getElementById('overall_times').innerHTML = red_time;
};
var interval = setInterval(function() {
counter();
}, 1000);
#overall_time {
display: none;
}
.div__time,
.total_time,
#overall_times {
display: inline-block;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="div__time">
<div id="overall_time"></div>
<div id="overall_times"></div> /
<div class="total_time">
00:00:10
</div>
</div>
<button onclick="start_over_all_time(this);" id="over_all_time">Over All Time</button>
This one working fine.
When I click a button I need to reset the countdown value to 0. For example if countdown time counting from 10 to 0 if click a button at count 5. then the count has to be reset to 0. This point only not working for me.
I'm using this code for reset the localstorage value
function start_over_all_time(button) {
var inputElemnets = '0';
localStorage.setItem("value", inputElemnets);
console.log(value);
}
Thanks in Advance.