如何使用下面的代码在同一个页面上创建多个倒计时器?

huangapple go评论87阅读模式
英文:

How to make multiple countdown timers at the same page using the codes below?

问题

如何在同一页上使用下面的代码创建多个倒计时器?
我尝试通过创建另一个变量 var start = document.getElementById("start2");var dis = document.getElementById("display2"); 来制作另一个倒计时器,但是当我点击第一个按钮时,只有第二个倒计时器在工作。

var start1 = document.getElementById("start1");
var dis1 = document.getElementById("display1");
var finishTime1;
var timerLength1 = 10;
var timeoutID1;
dis1.innerHTML = "" + timerLength1;

if (localStorage.getItem('myTime')) {
    Update();
}
start1.onclick = function () {
    localStorage.setItem('myTime', ((new Date()).getTime() + timerLength1 * 1000));
    if (timeoutID1 != undefined) window.clearTimeout(timeoutID1);
    Update();
}

function Update() {
    finishTime1 = localStorage.getItem('myTime');
    var timeLeft = (finishTime1 - new Date());
    dis1.innerHTML = "" + Math.max(timeLeft / 1000, 0);
    timeoutID1 = window.setTimeout(Update, 100);
}

var start2 = document.getElementById("start2");
var dis2 = document.getElementById("display2");
var finishTime2;
var timerLength = 100;
var timeoutID;
dis2.innerHTML = "" + timerLength;

if (localStorage.getItem('myTime')) {
    Update();
}
start2.onclick = function () {
    localStorage.setItem('myTime', ((new Date()).getTime() + timerLength * 1000));
    if (timeoutID != undefined) window.clearTimeout(timeoutID);
    Update();
}

function Update() {
    finishTime2 = localStorage.getItem('myTime');
    var timeLeft = (finishTime2 - new Date());
    dis2.innerHTML = "" + Math.max(timeLeft / 1000, 0);
    timeoutID = window.setTimeout(Update, 100);
}
<span id="display1"></span><button id="start1">START1</button>

<br><br>

<span id="display2"></span><button id="start2">START2</button>

请注意,您在代码中创建了两次 Update 函数,这会导致第一个倒计时器的 timeoutID 被第二个倒计时器的 timeoutID 覆盖,从而导致计时器行为不正确。您需要确保每个倒计时器都有独立的 Update 函数。

英文:

How to make multiple countdown timers at the same page using the codes below?
I tried to make another countdown timer by making another var start = document.getElementById("start2"); and var dis = document.getElementById("display2"); but when I click the 1 button only the second countdown timer is working,

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

 var start1 = document.getElementById(&quot;start1&quot;);
var dis1 = document.getElementById(&quot;display1&quot;);
var finishTime1;
var timerLength1 = 10;
var timeoutID1;
dis1.innerHTML = &quot;&quot; + timerLength1;
if(localStorage.getItem(&#39;myTime&#39;)){
Update();
}
start1.onclick = function () {
localStorage.setItem(&#39;myTime&#39;, ((new Date()).getTime() + timerLength1 * 1000));
if (timeoutID1 != undefined) window.clearTimeout(timeoutID1);
Update();
}
function Update() {
finishTime1 = localStorage.getItem(&#39;myTime&#39;);
var timeLeft = (finishTime1 - new Date());
dis1.innerHTML = &quot;&quot; + Math.max(timeLeft/1000,0)
timeoutID1 = window.setTimeout(Update, 100);
}
var start2 = document.getElementById(&quot;start2&quot;);
var dis2 = document.getElementById(&quot;display2&quot;);
var finishTime2;
var timerLength = 100;
var timeoutID;
dis2.innerHTML = &quot;&quot; + timerLength;
if(localStorage.getItem(&#39;myTime&#39;)){
Update();
}
start2.onclick = function () {
localStorage.setItem(&#39;myTime&#39;, ((new Date()).getTime() + timerLength * 1000));
if (timeoutID != undefined) window.clearTimeout(timeoutID);
Update();
}
function Update() {
finishTime2 = localStorage.getItem(&#39;myTime&#39;);
var timeLeft = (finishTime2 - new Date());
dis2.innerHTML = &quot;&quot; + Math.max(timeLeft/1000,0)
timeoutID = window.setTimeout(Update, 100);
}
&lt;!-- language: lang-html --&gt;
&lt;span id=&quot;display2&quot;&gt;&lt;/span&gt;&lt;button id=&quot;start1&quot;&gt;START1&lt;/button&gt; 
&lt;br&gt;&lt;br&gt;
&lt;span id=&quot;display2&quot;&gt;&lt;/span&gt;&lt;button id=&quot;start1&quot;&gt;START1&lt;/button&gt; 
&lt;!-- end snippet --&gt;
enter code here

答案1

得分: 0

你正在为localStorage使用相同的id。实际上,最好通过函数或类创建隔离的上下文,这样可以更容易地处理多个计数器。另外,你可以使用setInterval而不是setTimer

还有一个提示:当你将数字赋给字符串字段时,不需要写"" + value,因为数字会自动转换为字符串

const createCounter = (startElementId, displayElementId, localStorageId, timerLength) => {
   const startElement = document.getElementById(startElementId),
      displayElement = document.getElementById(displayElementId)
      
   let timeoutID

   displayElement.innerHTML = timerLength
   
   const storageValue = localStorage.getItem(localStorageId)
   if (storageValue) startTimer()

   startElement.onclick = () => {
      const value = (new Date()).getTime() + timerLength * 1000
      localStorage.setItem(localStorageId, value)
      startTimer()
   }
   
   function startTimer() {
      if (timeoutID) clearInterval(timeoutID)
      timeoutId = setInterval(updateTime, 100)
   }

   function updateTime(finishTime) {
      finishTime = finishTime || localStorage.getItem(localStorageId)
      const timeLeft = finishTime - new Date()
      displayElement.innerHTML = Math.max(timeLeft / 1000, 0)
   }      
}

// 为具有id start1和display1的元素运行第一个计时器
createCounter('start1', 'display1', 'someId1', 10)

// 为具有id start2和display2的元素运行第二个计时器
createCounter('start2', 'display2', 'someId2', 60)

HTML

<button id="start1">开始</button>
<div id="display1"></div>

<button id="start2">开始</button>
<div id="display2"></div>
英文:

You are using the same id for localStorage. Actually it is better to create isolated context through function or class, then it will be easier to handle as much counters as you wish. Also you can use setInterval instead of setTimer

One more tip: when you assign number to string field no need to write "" + value, because number authomatically will be converted into string

const createCounter = (startElementId, displayElementId, localStorageId, timerLength) =&gt; {
const startElement = document.getElementById(startElementId),
displayElement = document.getElementById(displayElementId)
let timeoutID
displayElement.innerHTML = timerLength
const storageValue = localStorage.getItem(localStorageId)
if (storageValue) startTimer()
startElement.onclick = () =&gt; {
const value = (new Date()).getTime() + timerLength * 1000
localStorage.setItem(localStorageId, value)
startTimer()
}
function startTimer () {
if (timeoutID) clearInterval(timeoutID)
timeoutId = setInterval(updateTime, 100)
}
function updateTime (finishTime) {
finishTime = finishTime || localStorage.getItem(localStorageId)
const timeLeft = finishTime - new Date()
displayElement.innerHTML = Math.max(timeLeft / 1000, 0)
}      
}
// run first timer for elements with ids start1 and display1
createCounter(&#39;start1&#39;, &#39;display1&#39;, &#39;someId1&#39;, 10)
// run second timer for elements with ids start1 and display1
createCounter(&#39;start2&#39;, &#39;display2&#39;, &#39;someId2&#39;, 60)

html

&lt;button id=start1&gt;Start&lt;/button&gt;
&lt;div id=display1&gt;&lt;/div&gt;

&lt;button id=start2&gt;Start&lt;/button&gt;
&lt;div id=display2&gt;&lt;/div&gt;

huangapple
  • 本文由 发表于 2020年9月22日 13:34:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/64003617.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定