JavaScript How to set Local Storage on Timer

When I refresh page timer is reset. I would like to set localStorage on this timer, how do it set in localStorage

HTML

00 : <span id="timer">02 : 00</span>

JavaScript :-

window.onload = function() {
    var min = "0" + 2;
    var sec = "0" + 0;
    setInterval(function() {
        var a = new Date();
        var time = min + " : " + sec;
        document.getElementById("timer").innerHTML = time;
        if (sec == 00) {
            if (min != 0) {
                min--;
                sec = 59;
                if (min < 10) {
                    min = "0" + min;
                }
            }
        } else {
            sec--;
            if (sec < 10) {
                sec = "0" + sec;
            }
        }
    }, 1000);
}
Answer:1

You can set Local storage like this:

1- On windows load check if you have already local storage for variables min and sec, if yes get both values using localStorage.getItem("item") else set both variables to start values.

2- Inside setInterval make sure that you are updating both local storage for min and sec while updating those variables.

Fiddle

Answer:2

I have a page with a background image and six tiles that are links, All of those are contained in a <div id = "main">. I also have a side navbar that pushes the content to the right when clicked ...

I have a page with a background image and six tiles that are links, All of those are contained in a <div id = "main">. I also have a side navbar that pushes the content to the right when clicked ...

I am working on Angular2 project and got an issue that Chrome caches HTML templates. It is not a problem for the development since I can make cache to be ignored in Chrome Dev mode. But it is a real ...

I am working on Angular2 project and got an issue that Chrome caches HTML templates. It is not a problem for the development since I can make cache to be ignored in Chrome Dev mode. But it is a real ...

I have an array of object. Each item has a paret id mentioned. I want to create a nested object where it nesting goes on as much as the subcategories. Input and expected output given below. How can I ...

I have an array of object. Each item has a paret id mentioned. I want to create a nested object where it nesting goes on as much as the subcategories. Input and expected output given below. How can I ...

Consider the following: function filterFoods() { var foods = [ {food: 'Bacon'}, {food: 'Butter'}, {food: 'Chicken'}, {food: 'Cocoa butter'}, {food: 'Saussages'}, {food: 'Ham'}, {food: ...

Consider the following: function filterFoods() { var foods = [ {food: 'Bacon'}, {food: 'Butter'}, {food: 'Chicken'}, {food: 'Cocoa butter'}, {food: 'Saussages'}, {food: 'Ham'}, {food: ...