I have an HTML5 offline (for work at the moment) video site that when the video ends, setInterval, switches to another video page. What I can't do is suspend the setInterval or the counter so that when the page is reloaded, the setting of the button-setInterval is saved in localStorage (which would mean not counting).
Could you help me?
<button id="auto" class="auto" onclick="cancelar()">
en <span class="counter">10</span> <span class="red">ó cancelar</span>
</button>
This is the code I use:
var seguir = false;
var boton = false;
var c =
document.querySelector(".auto");
c.addEventListener("click", cancelar);
function cancelar() {
if (!seguir) {
seguir = false;
clearInterval(yo);
//$(".auto").css({"background": "#ccc", "color": "#000"});
localStorage.setItem('boton-mode', false');
} else {
seguir = true;
localStorage.setItem('boton-mode', true);
};
};
And of course my counter works fine:
// AL FINALIZAR VÍDEO
var y = $(".yoFin");
$("#video").on("ended", function() {
y.show().css({"display": "flex"});
var counter = 10;
yo = window.setInterval( function() {
if (counter == 0) {
window.location.href = "yo.html";
} else {
counter = counter - 1;
document.querySelector(".counter").innerHTML = counter;
}
},1000);
});
Apparently I'm using pure JavaScript mixed with jQuery.
Although your code is incomplete (some HTML is missing) and the question is somewhat confusing (not very clear what you are trying to achieve), I will try to answer according to what I understand of the problem.
ISSUE
If you want to store a value in the object
localStorage
, it will be later captured when reloading a page or being redirected to another.The idea is to keep the state of a button up-to-date when playing media embedded in the page.
SOLUTION
The first thing we need to know is how the object works
localStorage
. It's very straightforward and simple: you set a key-value pair using the methodsetItem()
and get the value of a key using the methodgetItem()
.Using the object
localStorage
we can store the value of the state of the button, which I presume should be activated only at the end of the video playback and be active for a reasonable time before reloading or redirecting the page.The button will allow you to cancel the automatic action (cancel the
setInterval
) and prevent the page from reloading. At least that's what I think you want to achieve with this button. And that when reloading the page manually, the button saves the last state.Without knowing the content of the document you want to load or redirect to I can only assume that it is the same document (
yo.html
).In this document you could have the following
script
:With this you have basic functionality to store the state of the button in the object
localStorage
.To simulate this code, we are going to create our own element
localStorage
in the snippet , and simulate the redirection by looping the video. What I am interested in showing is the functionality of the code described above. Pressing the cancel button sets the value on the objectlocalStorage
tobotonActivo
afalse
.If I have understood the question correctly, this would be a way to stop the counter when the button is clicked
cancelar
, the state of the button would be saved in `localStorage``and when the page is reloaded, the state of the button would be established according to the stored value. That causes the automatic upload or redirection process to stop once the cancel button has been pressed.