在页面上显示计数计时器结果

时间:2018-01-23 16:19:51

标签: javascript html

我想取出计时器的结果,然后点击按钮在页面上显示。

1 个答案:

答案 0 :(得分:0)

这是我解决问题的方法,
我刚刚创建了一个简单的计数器,然后每次触发click事件时它都会显示当前的计时器值

代码:



//initialisation
var counterLabel = document.getElementById("counter");
var valueLabel = document.getElementById("value");
var totalSeconds = 0;
counterLabel.innerHTML = totalSeconds;
//the loop each 1s
setInterval(setTime, 1000);

//the function that will increase the conter value and save it into the cookie
function setTime() {
  ++totalSeconds;
  counterLabel.innerHTML = totalSeconds;
}

//to get the current conter value
function getTime() {
  valueLabel.innerHTML = totalSeconds;
}

<label id="counter"></label>
<button onclick="getTime()">save</button>
<label id="value"></label>
&#13;
&#13;
&#13;

小提琴:
https://jsfiddle.net/871t3w2o/3/

您还可以查看localstoragesessionstorage它们的工作方式与Cookie一样,但每个都有自己的优点和缺点,您应该选择满足您需求的那个

相关问题