简体   繁体   English

Point System - localStorage

[英]Point System - localStorage

I am creating a Point System, and once people get enough points, they can use some of the points to get cool features on my site. 我正在创建一个Point System,一旦人们获得足够的积分,他们就可以使用一些积分在我的网站上获得很酷的功能。 The using points part (detracting points each time the user gets something with their points) of the code is not working. 代码的使用点部分(每次用户获得点数时减少点数)都不起作用。 Here is my jQuery for the Point System: 这是我的点系统jQuery:

$(document).ready(function(){
   $('#curren').delay(200).animate({'top' : '-1px'},1500);
if (localStorage.points222){
  localStorage.points222=Number(localStorage.points222) +5;
}
else{
  localStorage.points222=0;
}
function buy5(){
  if (localStorage.points222){
  localStorage.points222=Number(localStorage.points222) -5;
}
else{
  localStorage.points222=0;
}
} 
   $('#point_counter').html(localStorage.points222);

if(Number(localStorage.points222) > 1){
   $('#point_name').html("1");
   $('#take').fadeIn();
}
else{
   $('#take').fadeOut();
} 
if(Number(localStorage.points222) > 500){
   $('#point_name').html("2");
}
if(Number(localStorage.points222) > 1000){
   $('#point_name').html("3");
}

if(Number(localStorage.points222) > 2000){
   $('#point_name').html("4");
}
if(Number(localStorage.points222) > 5000){
   $('#point_name').html("5");
}
if(Number(localStorage.points222) > 10000){
   $('#point_name').html("6");
}
if(Number(localStorage.points222) > 20000){
   $('#point_name').html("7");
}
});

and the HTML: 和HTML:

<div id="curren">
  Your Current Level: <b><span id="point_name"></span></b><br>
  Points: <b><span id="point_counter"></span></b>
  <span style="FLOAT:right;cursor:pointer;display:none;" id="take" onclick="buy5()">-5</span>
</div>

If you need more information on how the code is setup, please let me know. 如果您需要有关代码设置方式的更多信息,请告诉我们。

Short answer: you never update the displayed amount of points. 简答:您永远不会更新显示的积分数量。 You misplaced a line. 你错了一条线。

The value in localStorage is indeed decreased by 5 every time a user clicks on the <span> and buy5() is called, but .. you never update the displayed value of points. 每次用户点击<span>并且buy5()localStorage的值确实减少了5, 但是 ......你永远不会更新点的显示值。 In other words, the line.. 换句话说,这条线..

$('#point_counter').html(localStorage.points222);

.. should be inside the definition of buy5 , when right now it is one bracket too far. ..应该 buy5 的定义范围 buy5 ,现在它是一个支架太远了。 It's not being called at the end of every buy5() , instead it's called just once, as part of $(document).ready() . 它不会在每个buy5()结束时被调用,而是只调用一次,作为$(document).ready()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM