Show toolbar

2013年10月17日 星期四

Positive and Countdown Timer

標題:正倒數計時器
JavaScript (index.html):
<span id="loveboxy"></span>
<script type="text/javascript">
var TimerBoxy = function(e) {
    this.id = e.id;
    this.target = new Date(e.target);
};
TimerBoxy.prototype.show = function() {
    var s = Math.abs(parseInt( (new Date().getTime() - this.target.getTime() )/1000,10));
    var m = s/60;
    var h = m/60;
    var D = h/24;
    document.getElementById(this.id).innerHTML = 
    parseInt(D,10) + "日" + parseInt(h%24,10) + "時" + parseInt(m%60,10) + "分" + parseInt(s%60,10) + "秒";
};
TimerBoxy.prototype.start = function() {
    if(!this.timer) {
        var that = this;
        this.show();
        this.timer = window.setInterval(function() {
            that.show();
        }, 1000);
    }
};
TimerBoxy.prototype.stop = function() {
    if(this.timer) {
        window.clearInterval(this.timer);
        this.timer = null;
    }
};
loveboxy = new TimerBoxy({
    id : "loveboxy",
    target : "2011/11/11 11:11:11"
});
loveboxy.start();
</script>
使用範例:


說明:
當目標時間小於當日時間則為正數計時,反之則為倒數計時。
(感謝TonyQ幫忙修正。)

沒有留言:

張貼留言