javascript - Display current time from timer
问题描述
I have a timer which counts from o to 100 and display it in ptag with id countdowntimer
Im trying to display the current time when i click a button from the timer running.
also i want to reset the timer after button click.
how do i achieve it?
var timeleft = 1;
var downloadTimer = setInterval(function(){
timeleft++;
document.getElementById("countdowntimer").textContent = timeleft;
if(timeleft >= 100)
clearInterval(downloadTimer);
},1000);
function timenow()
{
var timenow=document.getElementById("timetaken").textContent = timeleft;
}
<p> Time <span id="countdowntimer">0 </span></p>
<p> Time Taken <span id="timetaken">0 </span></p>
<button onclick="timenow">Click to viw time taken</button>
解决方案
您的 onclick 函数事件未正确绑定。要重置计时器,只需将timeleft
按钮单击功能中的 var 重置为 0
var timeleft = 1;
var downloadTimer = setInterval(function(){
timeleft++;
document.getElementById("countdowntimer").textContent = timeleft;
if(timeleft >= 100)
clearInterval(downloadTimer);
},1000);
function timenow()
{
document.getElementById("timetaken").textContent = timeleft;
timeleft=0;
document.getElementById("countdowntimer").textContent = timeleft;
}
<p> Time <span id="countdowntimer">0 </span></p>
<p> Time Taken <span id="timetaken">0 </span></p>
<button onclick="timenow()">Click to viw time taken</button>
推荐阅读
- android - 完全禁用音频插孔作为媒体输出
- javascript - 有人能解释一下 React Native 语法“render() {...}”吗?
- firebase - 如何通过数据库更改 Firebase 身份验证密码
- android - 在滚动时显示和隐藏 BottomAppBar
- android - 在 android 7.1.x 上运行 python 2.x
- html - 在各种浏览器上以不同方式打印(在纸上)的图像
- php - 作曲家:找不到类,找不到我错的地方
- javascript - 如何检测两个div是否正在触摸/碰撞检测
- reactjs - Flatlist onEndReached 无限循环
- azure - Microsoft Azure Bot Framework Emulator 未发送消息