[英]problems with local storage and onClick on page refresh
hello guys i am woking on this app where there are 3 states on a button大家好,我正在使用这个应用程序,按钮上有 3 个状态
first state, this is the default when the web page is openend for the first time第一个 state,这是第一次打开 web 页面时的默认值
Picture 1:-图片1:-
then when you click it, it changes to another image, and sets a timer.然后当您单击它时,它会更改为另一个图像,并设置一个计时器。
Picture 2:-图2:-
when the timer is over it changes again to another image.当计时器结束时,它再次变为另一个图像。
Picture 3:-图3:-
then when u click it again it is changing to picture nr 2 with green outline and start timer again,然后当你再次点击它时,它会变成带有绿色轮廓的图片 nr 2 并再次启动计时器,
I use local storage to make sure I save the timer and the pictures, but my problem is when I refresh the page I turns back to picture number 1, here I would like it to remember what "state" it was in, if it was clicked once I need it to on refresh, remember that I should be "green" like on picture 2 and wait for the timer to end and turn "red" like on picture 3, and if the timer was out and it was red like on picture 3 I need it to remember that also!我使用本地存储来确保保存计时器和图片,但我的问题是当我刷新页面时,我会返回图片编号 1,在这里我希望它记住它所处的“状态”,如果它是单击一次我需要刷新时,请记住我应该像图 2 那样是“绿色”,然后等待计时器结束并像图 3 那样变成“红色”,如果计时器到了并且它像打开一样是红色的图 3 我也需要它记住这一点!
I'm REALLY lost, and all new to this, so I hoping for help, maybe and example..我真的迷路了,对这一切都是新手,所以我希望能得到帮助,也许还有例子……
thanks in advance:(:O提前谢谢:(:O
here a my html button code:这是我的 html 按钮代码:
<img class="toilet" src="~/lib/pictures/toilet.png" style="height:150px;width:150px;" />
and here is my javascript:这是我的 javascript:
<script>
$(document).ready(function () {
function aktivereSkift(
initImagePath = null,
initNextImagePath = null,
count = 15
) {
if (initImagePath === null || initNextImagePath === null) {
return false;
}
$(this).attr("src", initImagePath);
let timer = count * 1000;
let counter = setInterval(timerLS.bind(this, count), 1000);
function timerLS() {
count = count - 1;
localStorage.setItem("counter", count);
if (count <= 0) {
clearInterval(counter);
return;
}
}
setTimeout(() => $(this).attr("src", initNextImagePath), timer);
localStorage.setItem("nextimagepath", initNextImagePath);
localStorage.setItem("imagepath", initImagePath);
}
function loadDefaultValues() {
const initImagePath = localStorage.getItem("imagepath");
const initNextImagePath = localStorage.getItem("nextimagepath");
const counter = localStorage.getItem("counter");
aktivereSkift(initImagePath, initNextImagePath, counter);
}
loadDefaultValues();
$(".toilet").on("click", function () {
aktivereSkift.call(this, '/lib/pictures/toiletBegyndt.png', '/lib/pictures/toiletSlut.png');
});
});
</script>
HTML: HTML:
<img class="toilet" src="~/lib/pictures/toilet.png" style="height:150px;width:150px;" data-id="1"/>
<img class="toilet" src="~/lib/pictures/toilet.png" style="height:150px;width:150px;" data-id="2"/>
<img class="toilet" src="~/lib/pictures/toilet.png" style="height:150px;width:150px;" data-id="3"/>
JS: JS:
<script>
$(document).ready(function() {
var objects = JSON.parse(localStorage.getItem("objects") || "[]");
objects.forEach(function(object, index) {
if (localStorage.getItem("counter " + object.id) > 0) {
aktivereSkift(object.imagePath, object.nextImagePath, object.id, localStorage.getItem("counter " + object.id), true);
} else if (localStorage.getItem("counter " + object.id) == 0) {
aktivereSkift(object.imagePath, object.nextImagePath, object.id, localStorage.getItem("counter " + object.id), true);
} else {
loadDefaultValues();
}
});
function aktivereSkift(initImagePath = null, initNextImagePath = null, id = 0, count = 15, isRefresh = false) {
if (initImagePath === null || initNextImagePath === null) {
return false;
}
isRefresh ? $('.toilet[data-id="'+ id +'"]').attr("src", initImagePath) : $(this).attr("src", initImagePath);
let timer = count * 1000;
let counter = setInterval(timerLS.bind(this, count), 1000);
var objects = JSON.parse(localStorage.getItem("objects") || "[]");
var obj = {id: id, imagePath: initImagePath, nextImagePath: initNextImagePath }
const i = objects.findIndex(item => item.id === id);
if (i > -1) objects[i] = obj;
else objects.push(obj);
localStorage.setItem("objects", JSON.stringify(objects));
function timerLS() {
count = count - 1;
localStorage.setItem("counter " + id, count < 0 ? 0 : count);
if (count <= 0) {
clearInterval(counter);
return;
}
}
setTimeout(() => isRefresh ? $('.toilet[data-id="'+ id +'"]').attr("src", initNextImagePath) : $(this).attr("src", initNextImagePath), timer);
}
function loadDefaultValues() {
const initImagePath = localStorage.getItem("imagepath");
const initNextImagePath = localStorage.getItem("nextimagepath");
const counter = localStorage.getItem("counter");
aktivereSkift(initImagePath, initNextImagePath, counter);
}
$(".toilet").on("click", function() {
aktivereSkift.call(this, '/lib/pictures/toiletBegyndt.png', '/lib/pictures/toiletSlut.png', $(this).attr("data-id"));
});
});
</script>
Local storage survives page refresh.本地存储在页面刷新后仍然存在。 you don't need a backend server for this, it can be done using
JS
.您不需要为此使用后端服务器,可以使用
JS
来完成。 Have a method that sets up the content panel有一个设置内容面板的方法
function loadContent(state){
// get state as parameter and show buttons accordingly
}
on your $(document).ready
check is local storage holds those values ( localStorage.getItem()
) that you need.在您的
$(document).ready
上检查本地存储是否包含您需要的那些值( localStorage.getItem()
)。 if it does pass those if not pass initial setting如果它确实通过了那些如果没有通过初始设置
this i the code i have now, and it still refreshes other rows in my foreach loop这是我现在拥有的代码,它仍然刷新我的 foreach 循环中的其他行
@foreach (var item in Model)
{
<tr>
<td>
<img src="@String.Format("data:image/jpg;base64,{0}", Convert.ToBase64String(item.billede))" style="width:150px;height:150px; border-radius:15px;" />
</td>
<td>
<div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<img class="toilet" src="~/lib/pictures/toilet.png" style="height:150px;width:150px;" data-id="1" />
<img class="toilet" src="~/lib/pictures/toilet.png" style="height:150px;width:150px;" data-id="2" />
<img class="toilet" src="~/lib/pictures/toilet.png" style="height:150px;width:150px;" data-id="3" />
</div>
</td>
<td>
<div>
<a href="#" data-toggle="Behov" data-placement="bottom" title="Behov" data-content="@Html.DisplayFor(modelItem => item.behov)"><input type="image" src="~/lib/pictures/behov.png" style="height:150px;width:150px" /></a>
</div>
</td>
<td>
<div>
<a href="#" data-toggle="Sove" data-placement="bottom" title="Søvnbehov" data-content="@Html.DisplayFor(modelItem => item.sove)"><input type="image" src="~/lib/pictures/sove.png" style="height:150px;width:150px" /></a>
</div>
</td>
<td>
<div>
<a href="#" data-toggle="Div" data-placement="bottom" title="Diverse" data-content="@Html.DisplayFor(modelItem => item.div)"><input type="image" src="~/lib/pictures/diverse.png" style="height:150px;width:150px" /></a>
</div>
</td>
<td>
<div>
<a asp-action="Edit" asp-route-id="@item.Id"><input type="image" src="~/lib/pictures/Redigere.png" style="height:150px;width:150px" /></a>
</div>
</td>
<td>
<div>
<a asp-action="Delete" asp-route-id="@item.Id"><input type="image" src="~/lib/pictures/slet.png" style="height:150px;width:150px" /></a>
</div>
</td>
</tr>
<tr><td colspan="7"><h2><center>@Html.DisplayFor(modelItem => item.navn)</center></h2></td></tr>
}
</tbody>
@section scripts
{
<script>
$(document).ready(function () {
var objects = JSON.parse(localStorage.getItem("objects") || "[]");
objects.forEach(function (object, Index) {
if (localStorage.getItem("counter " + object.id) > 0) {
aktivereSkift(object.imagePath, object.nextImagePath, object.id, localStorage.getItem("counter " + object.id), true);
} else if (localStorage.getItem("counter " + object.id) == 0) {
aktivereSkift(object.imagePath, object.nextImagePath, object.id, localStorage.getItem("counter " + object.id), true);
} else {
loadDefaultValues();
}
});
function aktivereSkift(initImagePath = null, initNextImagePath = null, id = 0, count = 15, isRefresh = false) {
if (initImagePath === null || initNextImagePath === null) {
return false;
}
isRefresh ? $('.toilet[data-id="' + id + '"]').attr("src", initImagePath) : $(this).attr("src", initImagePath);
let timer = count * 1000;
let counter = setInterval(timerLS.bind(this, count), 1000);
var objects = JSON.parse(localStorage.getItem("objects") || "[]");
var obj = { id: id, imagePath: initImagePath, nextImagePath: initNextImagePath }
const i = objects.findIndex(item => item.id === id);
if (i > -1) objects[i] = obj;
else objects.push(obj);
localStorage.setItem("objects", JSON.stringify(objects));
function timerLS() {
count = count - 1;
localStorage.setItem("counter " + id, count < 0 ? 0 : count);
if (count <= 0) {
clearInterval(counter);
return;
}
}
setTimeout(() => isRefresh ? $('.toilet[data-id="' + id + '"]').attr("src", initNextImagePath) : $(this).attr("src", initNextImagePath), timer);
}
function loadDefaultValues() {
const initImagePath = localStorage.getItem("imagepath");
const initNextImagePath = localStorage.getItem("nextimagepath");
const counter = localStorage.getItem("counter");
aktivereSkift(initImagePath, initNextImagePath, counter);
}
$(".toilet").on("click", function () {
aktivereSkift.call(this, '/lib/pictures/toiletBegyndt.png', '/lib/pictures/toiletSlut.png', $(this).attr("data-id"));
});
});
</script>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.