gpt4 book ai didi

javascript - setInterval 在 Firefox 中无法正常工作

转载 作者:行者123 更新时间:2023-11-28 11:09:43 25 4
gpt4 key购买 nike

我对我的网站无限循环使用setInterval函数,但Firefox仅处理第一个间隔,对于下一个间隔,我网站的收藏夹图标更改为加载,而F5和刷新按钮则不会工作。

function printTime()
{
var now = new Date();
var hours = now.getHours();
var mins = now.getMinutes();
var sec = now.getSeconds();
document.write(hours + ":" + mins + ":" + sec);
}

setInterval("printTime()", 1000);

最佳答案

这完全在意料之中。不要使用 document.write apart during the initial loading of the page .

创建一个元素并将其附加到正文。

例如:

function printTime(){
var now = new Date();
var hours = now.getHours();
var mins = now.getMinutes();
var sec = now.getSeconds();
var txt = document.createTextNode(hours + ":" + mins + ":" + sec);
document.body.appendChild(txt);
}
setInterval(printTime, 1000); // note also the difference here : don't eval

请注意,几个小时后,您的页面会有点长。如果您想更改元素而不是附加元素,您可以这样做

document.getElementById('someId').innerHTML = hours + ":" + mins + ":" + sec; 

关于javascript - setInterval 在 Firefox 中无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22012240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com