如何在 setInterval 函数中使用 localStorage 存储十进制值,并将 "dot" 更改为 "comma"?

How can I store decimal value with localStorage in setInterval function and also change the "dot" to "comma"?

我正在尝试根据每秒创建 countUp 值。即使页面刷新,它也会继续计数(添加我的“customRate”值)。但是,如果我刷新页面,“点”之后的第二个值将不会恢复,它将重新从“customRate 值”开始。 有什么解决办法吗? 第二个问题是,如何将这段代码中的“点”改为“逗号”?

谢谢

这是我的代码:

var impulse = 0;
tmpImpulse = localStorage.getItem('impulseFix1')
if (tmpImpulse != null) {
  impulse = parseInt(tmpImpulse, 10);
}
setInterval(function() {
  localStorage.setItem('impulseFix1', impulse)
  customRate = 0.046
  impulse = impulse + customRate;
  document.getElementById("impulse").innerHTML = impulse.toFixed(3) + " impulse/second";
}, 1000);
<span id="impulse" style="color:red"> </span>

看这里 高兴时删除 let impulse = 0.5; // testing

并取消注释 localStorage 行

// let impulse = JSON.parse(localStorage.getItem('impulseFix1'));
let impulse = 0.5; // testing
const customRate = 0.046
if (impulse != null) {
  impulse = +impulse; // do not parseInt here, your decimals will go
}
setInterval(function() {
  impulse += customRate;
 //   localStorage.setItem('impulseFix1', JSON.stringify(impulse))
  document.getElementById("impulse").innerHTML = impulse.toFixed(3).replace(".",",") + " impulse/second";
}, 1000);
<span id="impulse" style="color:red"> </span>