倒数计数器,用户可在其中输入结束日期

时间:2015-07-06 18:54:32

标签: javascript html

我正在尝试创建倒计时器,用户可以在HTML页面中输入结束日期,但下面的代码允许用户仅在代码中输入日期。这里的计数器以window.onload事件开始,但我希望计数器在用户点击按钮时开始。

<head>
<title>JavaScript CountDown Timer</title>
<script type="text/javascript">
var ct = new Date(document.getElementById("endd").value);

function CountDown(initDate, id){
this.endDate = 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();
}

CountDown.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));
}

CountDown.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);
}

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

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

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

CountDown.prototype.updateCounter=function(){
this.calculate();
this.formatTime();
this.countainer.innerHTML ="<strong>" + this.years + "</strong> <small>" +  (this.years == 1? "year" : "years") + "</small>" +
   " <strong>" + this.months + "</strong> <small>" + (this.months == 1? "month" : "months") + "</small>" +
   " <strong>" + this.days + "</strong> <small>" + (this.days == 1? "day" : "days") + "</small>" +
   " <strong>" + this.hours + "</strong> <small>" + (this.hours == 1? "hour" : "hours") + "</small>" +
   " <strong>" + this.minutes + "</strong> <small>" + (this.minutes == 1? "minute" : "minutes") + "</small>" +
   " <strong>" + this.seconds + "</strong> <small>" + (this.seconds == 1? "second" : "seconds") + "</small>";
if ( this.endDate > (new Date()) ) {
    var self = this;
    setTimeout(function(){self.updateCounter();}, 1000);
}
}
var ct= "09/30/2015";
window.onload=function(){ new CountDown( ct, 'counter'); } 

</script>
</head>
<body>
<input type = "text" id="endd">
<div id="counter">Contents of this DIV will be replaced by Count Down</div>
</body>

1 个答案:

答案 0 :(得分:1)

我认为你需要做的就是删除window.onload=function()...,添加一个html按钮,然后使用Javascript将onclick listener附加到该按钮,当事件触发时调用new Countdown(...)就像你已经在做的那样。

jQuery倾向于使这种事情变得更容易,但是你可以在这里的vanilla JS中做到这一点。