JavaScript Bộ đếm ngược ngày giờ phút giây

<script type="text/javascript">
function DaysHMSCounter(initDate, id){
    this.counterDate = new Date(initDate);
    this.container = document.getElementById(id);
    this.update();
}
DaysHMSCounter.prototype.calculateUnit=function(secDiff, unitSeconds){
    var tmp = Math.abs((tmp = secDiff/unitSeconds)) < 1? 0 : tmp;
    return Math.abs(tmp < 0 ? Math.ceil(tmp) : Math.floor(tmp));
}
DaysHMSCounter.prototype.calculate=function(){
    var secDiff = Math.abs(Math.round(((new Date()) - this.counterDate)/1000));
    this.days = this.calculateUnit(secDiff,86400);
    this.hours = this.calculateUnit((secDiff-(this.days*86400)),3600);
    this.mins = this.calculateUnit((secDiff-(this.days*86400)-(this.hours*3600)),60);
    this.secs = this.calculateUnit((secDiff-(this.days*86400)-(this.hours*3600)-(this.mins*60)),1);
}
DaysHMSCounter.prototype.update=function(){
    this.calculate();
    this.container.innerHTML =
        " " + this.days + " " + (this.days == 1? "day" : "days") +
        " " + this.hours + " " + (this.hours == 1? "hour" : "hours") +
        " " + this.mins + " " + (this.mins == 1? "min" : "mins") +
        " " + this.secs + " " + (this.secs == 1? "sec" : "secs");
    var self = this;
    setTimeout(function(){self.update();}, (1000));
}
window.onload=function(){ new DaysHMSCounter('January 01, 2013 00:00:00', 'counter'); }
</script>
Tags: code mẫu code javascript đếm ngược
  0 Bình luận
Chia sẻ:  

Code Đơn Giản Giải Mã Kiểu Mã Hóa eval(base64_decode

Đăng kí nhận tin mới
Hãy để lại email của bạn, tôi sẽ thông báo với bạn khi có bài viết mới nhất.
Bạn đã không sử dụng site, Bấm vào đây để duy trì trạng thái đăng nhập. Thời gian chờ: 60 giây