0

I am implementing some custom timer which is purpose to count specified time. That timer needs to store counted time if user manually reload page. This parts works. But after specified time (in this case 5 seconds) user needs to be redirected.

For that purpose I using sessionStorage to keep current counted time. But after 5 second I am trying to remove item from sessionStorage and it doesn't work!

Where I a made mistake? How I can remove item from sessionStorage in my case? Thanks

 var currentTime = sessionStorage.getItem("currentTime");

        if (currentTime == null) {
            currentTime = 0;
        }

        setInterval(function () {
            currentTime++;
            if (currentTime == 5) {
                sessionStorage.removeItem("currentTime");

                location.reload();
            }                
            sessionStorage.setItem('currentTime', currentTime);               
        }, 1000);

1 Answer 1

1

The problem is that location.reload() doesn't terminate the execution of JavaScript. Therefore, after that line is ran, sessionStorage.setItem('currentTime', currentTime); is still run. To fix this issue, wrap that line in an else statement for the if (currentTime == 5).

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I also noticed. Probably I am tired and didn't see what stupied mistake I made. I solve like this: if (currentTime == 5) { currentTime = 0; location.reload(); }
I set currentTime = 0 when currentTime became target time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.