【发布时间】:2013-08-13 01:02:37
【问题描述】:
因此,我正在摆弄我一周前发现的这段代码,因为它能够“在某种程度上”完成旧 gmail 文件计数器的外观。我未能使计数器以比每秒更快的速度准确增加。
通过编辑代码中最低的 1000 [在 setTimeout(function(){thisobj.start()}, 1000) 中找到],计数器的计数速度要快得多,但是在刷新时它会恢复到它开始时的位置附近秒或两秒(取决于从开始到刷新的时间)。
<html>
<head>
<script type="text/javascript">
function countup(startingdate, base) {
this.currentTime = new Date();
this.startingdate = new Date(startingdate);
this.base = base;
this.start();
}
countup.prototype.oncountup = function(){};
countup.prototype.start = function() {
var thisobj = this,
timediff = (this.currentTime-this.startingdate) / 1000,
secondfield = Math.floor((timediff));
result = { seconds: secondfield };
this.currentTime.setSeconds(this.currentTime.getSeconds() + 1);
this.oncountup(result);
setTimeout(function(){
thisobj.start();
}, 1000);
};
</script>
</head>
<body>
<div id="holder"></div>
<script type="text/javascript">
var startDate = new countup("August 3, 2013 18:59:00", "seconds") // Change starting Date Here
startDate.oncountup= function(result) {
var mycountainer = document.getElementById("holder");
mycountainer.innerHTML =+ result['seconds'];
}
</script>
</body>
</html>
我相当肯定这可以更改为以毫秒为单位计数,从而以每秒一定数量的更快速度增加。
提前致谢!
【问题讨论】:
-
如果您最初编写了代码,这应该是微不足道的或者是大量的工作。但是,浏览器将无法满足您的要求。
-
这个问题似乎是题外话,因为它是关于修改提问者的代码(对未来的读者没有用)
标签: javascript milliseconds seconds