简体   繁体   English

innerHTML在setInterval上未更改

[英]innerHTML not changing on setInterval

In this javascript, "seconds left" should decrease every second but this do not happen. 在此javascript中,“剩余秒数”应每秒减少一次,但这不会发生。
innerhtml do not change the value for <span id='timer'> while innerHTML for <span id='id'> is working. <span id='id'> innerHTMLinnerhtml不会更改<span id='timer'>的值。

<script>
var lastdate=1373814606250;
var today=1373388292527;
var d,h,m,s;
function displayDate()
{
    today--;
    d=(lastdate-today)/86400000;
    h=(d-Math.floor(d))*24;
    m=(h-Math.floor(h))*60;
    s=(m-Math.floor(m))*60;
    document.getElementById("timer").innerHTML=Math.floor(d)+" Days "+Math.floor(h)+" hours "+Math.floor(m)+" minutes "+Math.floor(s)+" seconds";
    document.getElementById('id').innerHTML=today;
}
</script>
<body onload="setInterval('displayDate()',1000)">
<span id='timer'></span><br/>
<span id='id'></span>
</body>

See it Working here 看到它在这里工作

EDIT 编辑
Here today and lastdate is date in milliseconds. 这里的今天和最后的日期是毫秒。

I understood There was logical error in calculating seconds. 我了解在计算秒数时存在逻辑错误。
It should be 它应该是

today=today+1000;

Because of milliseconds not seconds 因为毫秒而不是秒

The innerHTML of the first span works too but the content never changes. 第一个范围的innerHTML也可以使用,但是内容永远不会改变。 Math.floor(d)+" Days "+Math.floor(h)+" hours "+Math.floor(m)+" minutes "+Math.floor(s)+" seconds" gives every time the same result. Math.floor(d)+" Days "+Math.floor(h)+" hours "+Math.floor(m)+" minutes "+Math.floor(s)+" seconds"每次给出相同的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM