猿问

如何在 localStorage 到期之前停止倒计时?

以下脚本进行倒计时,直到达到设定的数字(在本例中为 76),然后再次开始倒计时,依此类推。更新页面倒计时不会重置,直到 localStorage 到期。


我希望当倒计时达到 76 时它停止并且不再开始计数。这直到 localStorage 过期。请问我该怎么做?


<div id="divCounter"></div>

var min = 80;

var max = 85;

var random = Math.floor(Math.random() * (+max - +min)) + +min;


if (localStorage.getItem("counter")) {

  if (localStorage.getItem("counter") >= 1000000000) { // Duration of localStorage

    var value = random;

  } else {

    var value = localStorage.getItem("counter");

  }

} else {

  var value = random;

}


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


var counter = function () {

  if (value <= 76) { // End count down. <= or >=

    localStorage.setItem("counter", random);

    value = random;

  } else {

    value = parseInt(value) - 1; // + 1 or - 1

    localStorage.setItem("counter", value);

  }


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

};


var interval = setInterval(function () {

  counter();

}, 1000); // speed count


慕哥6287543
浏览 161回答 2
2回答

芜湖不芜

这段代码有很多问题,但考虑到这里的情况,您需要在 value <= 76 时调用 clearInterval(interval) 以阻止 localStorage 在该点再次更改。

天涯尽头无女友

这是我对您意图的最佳猜测:// If there's a count in localStorage but it is expired then//&nbsp; &nbsp; &nbsp; set value to a random between 80 and 85// else//&nbsp; &nbsp; &nbsp; set value to the count in localStorage// Display the count in a div and decrease it by 1 every second.&nbsp;// If the count falls below 76 then&nbsp;//&nbsp; &nbsp; &nbsp; stop counting down and//&nbsp; &nbsp; &nbsp; reset the localStorage counter to the random value&nbsp; &nbsp; &nbsp; &nbsp; const min = 80;&nbsp; &nbsp; &nbsp; &nbsp; const max = 85;&nbsp; &nbsp; &nbsp; &nbsp; const stopValue = 76;&nbsp; &nbsp; &nbsp; &nbsp; const duration = 1000000000;&nbsp; &nbsp; &nbsp; &nbsp; const rnd = Math.floor(Math.random() * (max - min)) + min;&nbsp; &nbsp; &nbsp; &nbsp; const counter = () => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; const lsCounter = localStorage.getItem('counter');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; let retval;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(lsCounter && lsCounter <= duration){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retval = lsCounter - 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(lsCounter <= stopValue){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; clearInterval(interval);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retval = rnd;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; retval = rnd;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; localStorage.setItem('counter', retval);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; document.getElementById('divCounter').innerHTML = retval;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; let interval = setInterval(function () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; counter();&nbsp; &nbsp; &nbsp; &nbsp; }, 1000);&nbsp;这样对吗?
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答