gpt4 book ai didi

Javascript计数计时器修改

转载 作者:行者123 更新时间:2023-12-02 19:54:06 27 4
gpt4 key购买 nike

我终于找到了一个不错的 javascript 计数计时器,没有多余的装饰。我想启动多个具有不同初始时间的计时器实例来进行计数。到目前为止,我取得的唯一进展就是打破了剧本。

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>JavaScript CountUp Timer - Praveen Lobo</title>
<script type="text/javascript">
/**********************************************************************************************
* CountUp script by Praveen Lobo (http://PraveenLobo.com/techblog/javascript-countup-timer/)
* This notice MUST stay intact(in both JS file and SCRIPT tag) for legal use.
* http://praveenlobo.com/blog/disclaimer/
**********************************************************************************************/
function CountUp(initDate, id){
this.beginDate = new Date(initDate);
this.countainer = document.getElementById(id);
this.numOfDays = [ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 ];
this.borrowed = 0, this.years = 0, this.months = 0, this.days = 0;
this.hours = 0, this.minutes = 0, this.seconds = 0;
this.updateNumOfDays();
this.updateCounter();
}

CountUp.prototype.updateNumOfDays=function(){
var dateNow = new Date();
var currYear = dateNow.getFullYear();
if ( (currYear % 4 == 0 && currYear % 100 != 0 ) || currYear % 400 == 0 ) {
this.numOfDays[1] = 29;
}
var self = this;
setTimeout(function(){self.updateNumOfDays();}, (new Date((currYear+1), 1, 2) - dateNow));
}

CountUp.prototype.datePartDiff=function(then, now, MAX){
var diff = now - then - this.borrowed;
this.borrowed = 0;
if ( diff > -1 ) return diff;
this.borrowed = 1;
return (MAX + diff);
}

CountUp.prototype.calculate=function(){
var currDate = new Date();
var prevDate = this.beginDate;
this.seconds = this.datePartDiff(prevDate.getSeconds(), currDate.getSeconds(), 60);
this.minutes = this.datePartDiff(prevDate.getMinutes(), currDate.getMinutes(), 60);
this.hours = this.datePartDiff(prevDate.getHours(), currDate.getHours(), 24);
this.days = this.datePartDiff(prevDate.getDate(), currDate.getDate(), this.numOfDays[currDate.getMonth()]);
this.months = this.datePartDiff(prevDate.getMonth(), currDate.getMonth(), 12);
this.years = this.datePartDiff(prevDate.getFullYear(), currDate.getFullYear(),0);
}

CountUp.prototype.addLeadingZero=function(value){
return value < 10 ? ("0" + value) : value;
}

CountUp.prototype.formatTime=function(){
this.seconds = this.addLeadingZero(this.seconds);
this.minutes = this.addLeadingZero(this.minutes);
this.hours = this.addLeadingZero(this.hours);
}

CountUp.prototype.updateCounter=function(){
this.calculate();
this.formatTime();
this.countainer.innerHTML =
"<strong>" + this.days + "</strong> " +
"<strong>" + this.hours + "</strong>:" +
"<strong>" + this.minutes + "</strong>:" +
"<strong>" + this.seconds + "</strong>";
var self = this;
setTimeout(function(){self.updateCounter();}, 1000);
}

window.onload=function(){ new CountUp(new Date(), 'counter'); }

</script>
</head>
<body>
<div id="counter">Contents of this DIV will be replaced by the timer</div>
</body>
</html>

最佳答案

每次按下按钮时,此代码都会向您的 HTML 添加一个新计数器:

var counterNum = 1;    

function addCounter() {
var div = document.createElement("div");
div.id = "counter" + counterNum++;
document.body.appendChild(div);
var x = new CountUp(new Date(), div.id);
}

使用此 HTML 作为按钮:

<button onclick="addCounter()">Add Counter</button>

在此处查看工作演示:http://jsfiddle.net/jfriend00/NshSf/ .

或者,如果您想要不同的初始时间,您可以这样设置初始时间:

 new CountUp(new Date() - (10*60*60*1000), div.id);   // start 10 minutes ago (time in ms)

您可以see here使用此代码按下按钮添加的计时器从 10 分钟开始。

关于Javascript计数计时器修改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8887510/

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