我使用setInterval
函数用于我的网站无限循环,但Firefox只处理第一个间隔,对于下一个间隔,我网站的fav图标更改为加载,而F5和刷新按钮不起作用。< / p>
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);
答案 0 :(得分:11)
这完全是预期的。请勿使用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;
答案 1 :(得分:1)
删除document.write
。试试这样的事情
$(document).ready(function () {
function printTime() {
var now = new Date();
var hours = now.getHours();
var mins = now.getMinutes();
var sec = now.getSeconds();
$("#myDiv").html(hours + ":" + mins + ":" + sec);
}
setInterval(printTime, 1000);
});
<div id="myDiv"></div>
答案 2 :(得分:0)
<div id="test">Hello</div>
在文档中添加上面的html并运行此脚本
var flag = false;
function printTime()
{
var now = new Date();
var hours = now.getHours();
var mins = now.getMinutes();
var sec = now.getSeconds();
// document.write(hours + ":" + mins + ":" + sec);
if(flag==true)
{
document.getElementById("test").backgroundColor="green";
flag = false;
}else
{
document.getElementById("test").backgroundColor="red";
flag = true;
}
}
setInterval("printTime()", 1000);
如果它不起作用那么balim Firefox :)虽然它肯定会起作用
答案 3 :(得分:0)
试试这个简单的例子:
function printTime()
{
var now = new Date();
var hours = now.getHours();
var mins = now.getMinutes();
var sec = now.getSeconds();
var div = document.getElementById('divID').innerHTML = hours + ":" + mins + ":" + sec;
}
setInterval(printTime(), 1000);