简体   繁体   English

Onclick更改HTML5本地存储值

[英]Onclick change HTML5 localstorage value

I'm using count down timer by this code. 我正在通过此代码使用倒数计时器。 If the page refresh or reload the count should not be reset for that I'm using this from localstorage. 如果页面刷新或重新加载,则不应为此重置计数,因为我正在使用localstorage进行计数。 If there any alternate solution for this means Please suggest me. 如果有其他替代解决方法,请提出建议。

 var hms = $(".div__time .total_time").text(); var a = hms.split(':'); var hrs_min_sec = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]); var time_hrs_min_sec = hrs_min_sec; if (localStorage.getItem("counter")) { if (localStorage.getItem("counter") <= 0) { var value = time_hrs_min_sec; } else { var value = localStorage.getItem("counter"); } } else { var value = time_hrs_min_sec; } document.getElementById('overall_time').innerHTML = value; var counter = function() { if (value <= 0) { localStorage.setItem("counter", time_hrs_min_sec); } else { value = parseInt(value) - 1; console.log(value); localStorage.setItem("counter", value); } document.getElementById('overall_time').innerHTML = value; if (value == 0) { // var redirect_url = "<?php echo site_url('home'); ?>"; // window.location.href = redirect_url; } var hours = Math.floor(value / 3600); var minutes = Math.floor(value % 3600 / 60); var seconds = Math.floor(value % 3600 % 60); var red_time = hours + ' : ' + minutes + ' : ' + seconds; document.getElementById('overall_times').innerHTML = red_time; }; var interval = setInterval(function() { counter(); }, 1000); 
 #overall_time { display: none; } .div__time, .total_time, #overall_times { display: inline-block; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="div__time"> <div id="overall_time"></div> <div id="overall_times"></div> / <div class="total_time"> 00:00:10 </div> </div> <button onclick="start_over_all_time(this);" id="over_all_time">Over All Time</button> 

This one working fine. 这个工作正常。

When I click a button I need to reset the countdown value to 0. For example if countdown time counting from 10 to 0 if click a button at count 5. then the count has to be reset to 0. This point only not working for me. 当我单击按钮时,我需要将倒计时值重置为0。例如,如果在5点单击一个按钮,则倒数时间从10计数到0,则必须将计数重置为0。这仅对我不起作用。

I'm using this code for reset the localstorage value 我正在使用此代码重置本地存储值

function start_over_all_time(button) {
    var inputElemnets = '0';
    localStorage.setItem("value", inputElemnets);
    console.log(value);
}

Fiddle Link 小提琴链接

Thanks in Advance. 提前致谢。

Okay... To objective here is to make the countdown "set to zero" button working. 好吧...这里的目标是使倒数“设置为零”按钮起作用。

Since SO snippet do not allow localStorage, the working scrip is on CodePen . 由于SO代码段不允许使用localStorage,因此工作代码位于CodePen上

The main issue was not having declared the value at global scope. 主要问题是尚未在全球范围内宣布其value
See explanations within the code. 请参阅代码中的说明。

// I removed the 3 lines below because that was the only use of jQuery afer all...
// And because the math is weird to read (and incorrect).

//var hms = $(".div__time .total_time").text();
//var a = hms.split(':');
//var hrs_min_sec = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);

// Replaced by this:
var hms = document.querySelector(".total_time").innerHTML;
var hms_arr = hms.split(":");
var time_hrs_min_sec = (hms_arr[0]*3600) + (hms_arr[1]*60) + hms_arr[2];

// Declare the "value" used in almost all functions at the global scope.    
var value;

if (localStorage.getItem("counter")) {
  if (localStorage.getItem("counter") <= 0) {
    value = time_hrs_min_sec;  // Removed the var
  } else {
    value = localStorage.getItem("counter");  // Removed the var
  }
} else {
  value = time_hrs_min_sec;  // Removed the var
}

document.getElementById('overall_time').innerHTML = value;

var counter = function() {
  if (value <= 0) {
    localStorage.setItem("counter", time_hrs_min_sec);
  } else {
    value = parseInt(value) - 1;
    console.log(value);
    localStorage.setItem("counter", value);
  }
  document.getElementById('overall_time').innerHTML = value;


  if (value == 0) {
    // var redirect_url = "<?php echo site_url('home'); ?>";
    // window.location.href = redirect_url;
  }

  var hours = Math.floor(value / 3600);
  var minutes = Math.floor(value % 3600 / 60);
  var seconds = Math.floor(value % 3600 % 60);
  var red_time = hours + ' : ' + minutes + ' : ' + seconds;
  document.getElementById('overall_times').innerHTML = red_time;
};
var interval = setInterval(function() {
  counter();
}, 1000);

// Use value here... Instead of inputElemnets
function start_over_all_time(button) {
  value = 0;
  localStorage.setItem("counter", value);
  console.log(value);
  // Be cool with the browser and stop the interval.
  setTimeout(function(){
    clearInterval(interval);
  },1000);
}

Now the set to zero button works... because you use the value variable everywhere. 现在,设置为零的按钮可以工作了……因为您到处都使用了value变量。 So as soon as you set it to zero, in start_over_all_time() , the next iteration of the interval will do the rest. 因此,一旦在start_over_all_time()中将其设置为零,则间隔的下一次迭代将完成其余工作。

There is plenty other things to fix... But that was your question. 还有很多其他问题需要解决...但这是您的问题。

When it comes to caching user data, you have 2 options: 在缓存用户数据时,您有2个选项:

  • HTML5 storage (no backend required) => LocalStorage or SessionStorage HTML5存储(不需要后端)=> LocalStorage或SessionStorage
  • Cookies (backend required) Cookies(需要后端)

LocalStorage or SessionStorage LocalStorage或SessionStorage

Instead of LocalStorage you could use SessionStorage. 可以使用SessionStorage代替LocalStorage。 The difference is, that it lives only as long as the browser session is open, ie closing the browser will end the session and delete the storage. 所不同的是,它仅在打开浏览器会话时才有效,即关闭浏览器将结束该会话并删除存储。

It's up to you, which of the both you want to use. 由您决定,要使用两者中的哪一个。 For more, check out this SO question . 有关更多信息,请查看此SO问题

Cookies 饼干

Cookies are primarily for reading server-side, local storage can only be read by the client-side. Cookies主要用于读取服务器端,本地存储只能由客户端读取。

If I understand you correctly, you only want to save some UI state rather than have the server/backend remember this state. 如果我理解正确,那么您只想保存一些UI状态,而不希望服务器/后端记住该状态。 In comparison to html5 storage, cookies are: 与html5存储相比,cookies为:

  • smaller
  • have an expiration date 有到期日
  • are harder to implement (as it requires a backend) 难以实现(因为它需要后端)

For more, check out this SO question . 有关更多信息,请查看此SO问题

Edit: Cookies can actually be set and read by js alone, thanks for correcting me @jon-p 编辑:Cookies可以实际上由js单独设置和读取,感谢您纠正我@ jon-p

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

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