我正在为学术目的创建几页,并且我想将“下一步”按钮隐藏一段时间。问题在于,一旦刷新页面,计时器就会重新开始。我希望它在刷新页面后继续,并且我想改进已经编写的代码,而不是看到新的解决方案。
<strong>
<input class="btn btn-primary btn-large btn-primary next-button" type="submit" value="Next"/>
<script>
var showButton = document.getElementsByClassName("btn btn-primary btn-large btn-primary next-button")[0];
var counter = 5;
var newElement = document.createElement("p");
newElement.innerHTML = "You can proceed to the subsequent page in " + counter + " seconds.";
var interval;
showButton.parentNode.replaceChild(newElement, showButton);
interval = setInterval(function() {
counter--;
if(counter < 0) {
newElement.parentNode.replaceChild(showButton, newElement);
clearInterval(interval);
} else {
newElement.innerHTML = "You can proceed to the subsequent page in " + counter.toString() + " seconds.";
}
}, 1000);
</script>
</strong>
我确实了解localStorage背后的想法,但是我到底需要在哪里插入它?我正在尝试将其插入“变量计数器”旁边,但它没有执行我想要的操作。
Javascript是客户端的。它将在reload或任何其他东西上进行reset。
您的问题的简单解决方案可能是HTML5 storage
或session storage
示例:
// Store localStorage.setItem("lastname", "Smith"); // Retrieve document.getElementById("result").innerHTML = localStorage.getItem("lastname");
HTML Web存储提供了两个用于在客户端上存储数据的对象:
window.localStorage
-存储没有到期日期的数据window.sessionStorage
-存储一个会话的数据(关闭浏览器选项卡时数据丢失)]localStorage对象
localStorage对象存储没有到期日期的数据。当浏览器关闭时,数据不会被删除,并且在第二天,每周或每年都可用。
有关LocalStorage Here的更多文档
您需要localStorage
或cookies
来存储数据,甚至在页面重新加载后也要使用。