Reputation: 5332
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);
Upvotes: 0
Views: 216
Reputation: 26
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)
.
Upvotes: 1