最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - setInterval doesn't work correctly in firefox - Stack Overflow

programmeradmin1浏览0评论

i use setInterval function for my website infinite loop, but Firefox only handles the first interval and for next one, the fav icon of my site changes to loading, and F5 and refresh button don't work.

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);

i use setInterval function for my website infinite loop, but Firefox only handles the first interval and for next one, the fav icon of my site changes to loading, and F5 and refresh button don't work.

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);
Share Improve this question edited Feb 25, 2014 at 11:13 Reger 4745 silver badges17 bronze badges asked Feb 25, 2014 at 11:03 BlackMBBlackMB 2286 silver badges20 bronze badges 2
  • 2 document.write? SRSLY? – Joseph Commented Feb 25, 2014 at 11:05
  • document.write on a page that's not open for writing will invoke document.open, and document.open clears the page. – Paul S. Commented Feb 25, 2014 at 11:06
Add a ment  | 

4 Answers 4

Reset to default 11

That's totally expected. Don't use document.write apart during the initial loading of the page.

Create an element and append it to the body instead.

For example :

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

Note that after a few hours, your page will be a little long. If you wanted to change an element instead of appending, you'd do

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

Remove document.write. try something like this

$(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>
<div id="test">Hello</div>

add above html in your document and run this script

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);

if it doesn't work then balim Firefox :) although it will work surely

Try this simple example:

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);
发布评论

评论列表(0)

  1. 暂无评论