Javascript 倒计时计时器:无法设置属性';文本内容';空的
我在倒计时脚本中出错了。在这里找不到解决方案。错误消息每秒产生1个错误:Javascript 倒计时计时器:无法设置属性';文本内容';空的,javascript,timer,web,countdown,Javascript,Timer,Web,Countdown,我在倒计时脚本中出错了。在这里找不到解决方案。错误消息每秒产生1个错误: Uncaught TypeError: Cannot set property 'textContent' of null at (index):181 (anonymous) @ (index):181 setInterval (async) (anonymous) @ (index):179 这是计时器的脚本: <script type="text/javascript"> var timeleft =
Uncaught TypeError: Cannot set property 'textContent' of null
at (index):181
(anonymous) @ (index):181
setInterval (async)
(anonymous) @ (index):179
这是计时器的脚本:
<script type="text/javascript">
var timeleft = 45;
var downloadTimer = setInterval(function(){
timeleft--;
document.getElementById("countdowntimer").textContent = timeleft;
document.getElementById("countdowntimer").style.color = "white";
if(timeleft <= 0)
clearInterval(downloadTimer);
},1000);
</script>
var timeleft=45;
var downloadTimer=setInterval(函数(){
时间限制--;
document.getElementById(“倒计时”).textContent=timeleft;
document.getElementById(“倒计时”).style.color=“白色”;
如果(timeleft您的html(根据您的评论)无效(
)。此外,代码可以简化
nofticks倒计时(5);
nOfTicks(nOfTicks)的函数倒计时{
document.querySelector(“倒计时”).textContent=nOfTicks--;
//[必要时多做些事情]
如果(nOfTicks>=0){
返回setTimeout(()=>nOfTicks(nOfTicks)的倒计时,1000);
}
返回刷新();
}
函数刷新(){
document.querySelector(“div.timer”).textContent+=“完成”;
}
在中刷新
45秒
HTML中的textContent
元素是什么?DOM中没有id为CountdowntTimer
的元素。可能的重复项您确定文档中有id为“CountdowntTimer”的元素吗?如果是,它可能还没有加载,可以尝试使用document.addEventListener(“DOMContentLoaded”),函数包围您的代码(event){//CODE}
在45秒后刷新
是我尝试加载脚本的地方。它可以工作,但会产生这些错误非常感谢您快速简单的回答,仍然会产生错误,但只有一次。