Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/475.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript计数计时器修改_Javascript_Date_Timer_Counter - Fatal编程技术网

Javascript计数计时器修改

Javascript计数计时器修改,javascript,date,timer,counter,Javascript,Date,Timer,Counter,我终于找到了一个不错的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

我终于找到了一个不错的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>

JavaScript倒计时-Praveen Lobo
/**********************************************************************************************
*Praveen Lobo的倒计时脚本(http://PraveenLobo.com/techblog/javascript-countup-timer/)
*此通知必须保持完整(在JS文件和脚本标记中),以便合法使用。
* http://praveenlobo.com/blog/disclaimer/
**********************************************************************************************/
函数倒计时(initDate,id){
this.beginDate=新日期(initDate);
this.countainer=document.getElementById(id);
this.numOfDays=[31,28,31,30,31,30,31,31,30,31];
this.followed=0,this.years=0,this.months=0,this.days=0;
this.hours=0,this.minutes=0,this.seconds=0;
此.updateEnumOfDays();
this.updateCounter();
}
CountUp.prototype.updateEnumOfDays=函数(){
var dateNow=新日期();
var currYear=dateNow.getFullYear();
如果((当前年份%4==0和当前年份%100!=0)| |当前年份%400==0){
这个.numOfDays[1]=29;
}
var self=这个;
setTimeout(函数(){self.updateEnumOfDays();},(新日期((currYear+1),1,2)-dateNow));
}
CountUp.prototype.datePartDiff=函数(当时、现在、最大值){
var diff=现在-然后-这个.followed;
这个值=0;
if(diff>-1)返回diff;
这个值=1;
返回(最大+差异);
}
CountUp.prototype.calculate=函数(){
var currDate=新日期();
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.numfdays[currDate.getMonth());
this.months=this.datePartDiff(prevDate.getMonth(),currDate.getMonth(),12);
this.years=this.datePartDiff(prevDate.getFullYear(),currDate.getFullYear(),0);
}
CountUp.prototype.addLeadingZero=函数(值){
返回值<10?(“0”+值):值;
}
CountUp.prototype.formatTime=函数(){
this.seconds=this.addLeadingZero(this.seconds);
this.minutes=this.addLeadingZero(this.minutes);
this.hours=this.addLeadingZero(this.hours);
}
CountUp.prototype.updateCounter=函数(){
这个.calculate();
这个.formatTime();
this.countainer.innerHTML=
“”+this.days+“”+
“”+this.hours+”:”+
“”+this.minutes+”:”+
“”+this.seconds+“”;
var self=这个;
setTimeout(函数(){self.updateCounter();},1000);
}
window.onload=function(){new CountUp(new Date(),'counter');}
此DIV的内容将被计时器替换

每次按下按钮时,此代码将向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>

您可以使用此代码在10分钟内按下按钮开始添加计时器。

如果您愿意,也可以使用多个计数器预先填充页面。CountUp函数接受一个日期对象参数,该参数表示它的计数截止日期<代码>新日期()将始终返回当前时间。在下面的示例中,我刚刚创建了几个
元素,并实例化了几个具有不同日期的
CountUp
函数。我所做的更改在底部

<!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(){
    var counters = [
        new CountUp(new Date(), 'counter1'), // Today
        new CountUp(new Date(2012, 0, 1), 'counter2'), // Jan 1 2012
        new CountUp(new Date(2011, 11, 25), 'counter3') // Dec 25, 2011
    ];      
}

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

JavaScript倒计时-Praveen Lobo
/**********************************************************************************************
*Praveen Lobo的倒计时脚本(http://PraveenLobo.com/techblog/javascript-countup-timer/)
*此通知必须保持完整(在JS文件和脚本标记中),以便合法使用。
* http://praveenlobo.com/blog/disclaimer/
**********************************************************************************************/
函数倒计时(initDate,id){
this.beginDate=新日期(initDate);
this.countainer=document.getElementById(id);
this.numOfDays=[31,28,31,30,31,30,31,31,30,31];
this.followed=0,this.years=0,this.months=0,this.days=0;
this.hours=0,this.minutes=0,this.seconds=0;
此.updateEnumOfDays();
this.updateCounter();
}
CountUp.prototype.updateEnumOfDays=函数(){
var dateNow=新日期();
var currYear=dateNow.getFullYear();
如果((当前年份%4==0和当前年份%100!=0)| |当前年份%400==0){
这个.numOfDays[1]=29;
}
var self=这个;
setTimeout(函数(){self.updateEnumOfDays();},(新日期((currYear+1),1,2)-dateNow));
}
CountUp.prototype.datePartDiff=函数(当时、现在、最大值){
var diff=现在-然后-这个.followed;
这个值=0;
if(diff>-1)返回diff;
这个值=1;
返回(最大+差异);
}
CountUp.prototype.calculate=函数(){
var currDate=新日期();
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.numfdays[currDate.getMonth());
this.months=this.datePartDiff(prevDate.getMonth(),currDate.getMonth(),12);
this.years=this.datePartDiff(prevDate.getFullYear(),currDate.getFullYear(),0);
}
CountUp.prototype.addLeadingZero=函数(值){
返回值<10?(“0”+值):值;
}
CountUp.prototype.formatTime=函数(){
this.seconds=this.addLeadingZero(此
<!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(){
    var counters = [
        new CountUp(new Date(), 'counter1'), // Today
        new CountUp(new Date(2012, 0, 1), 'counter2'), // Jan 1 2012
        new CountUp(new Date(2011, 11, 25), 'counter3') // Dec 25, 2011
    ];      
}

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