var cout = { init:function (classname) { var $this = this if (classname) { var classname = classname } else { var classname = ".bb" } $(document).delegate(classname, "click", function () { var to_time = new Date(Date.parse($(this).text())) $this.pri(this, to_time) }) }, last_time:function () { var $this = this var now = new Date(); $this.long = $this.to_time.getTime() - now.getTime() $this.day = parseInt($this.long / (1000 * 60 * 60 * 24)) $this.hours = parseInt($this.long % (1000 * 60 * 60 * 24) / (1000 * 60 * 60)) $this.min = parseInt($this.long % (1000 * 60 * 60) / (1000 * 60)) $this.sec = parseInt($this.long % (1000 * 60) / 1000) $this.text=$this.day + "天" + $this.hours + "小时" + $this.min + "分钟" + $this.sec + "秒" }, pri:function (obj, time) { var $this = this function temp(){ $this.to_time = time $this.last_time() $(obj).html($this.text) window.setTimeout(function () { temp() }, 1000) } temp() } } cout.init(".time")
或者
var cout = { init:function (classname) { var $this = this if (classname) { var classname = classname } else { var classname = ".bb" } $(document).delegate(classname, "click", function () { var to_time = new Date(Date.parse($(this).text())) $this.pri(this, to_time)() }) }, last_time:function () { var $this = this var now = new Date(); $this.long = $this.to_time.getTime() - now.getTime() $this.day = parseInt($this.long / (1000 * 60 * 60 * 24)) $this.hours = parseInt($this.long % (1000 * 60 * 60 * 24) / (1000 * 60 * 60)) $this.min = parseInt($this.long % (1000 * 60 * 60) / (1000 * 60)) $this.sec = parseInt($this.long % (1000 * 60) / 1000) $this.text = $this.day + "天" + $this.hours + "小时" + $this.min + "分钟" + $this.sec + "秒" }, pri:function (obj, time) { var $this = this return function () { $this.to_time = time $this.last_time() $(obj).html($this.text) var temp = arguments.callee window.setTimeout(function () { temp() }, 1000) } } } cout.init(".time")
或者
var cout = { init:function (classname) { var $this = this if (classname) { var classname = classname } else { var classname = ".bb" } $(document).delegate(classname, "click", function () { var to_time = new Date(Date.parse($(this).text())) $this.pri(this, to_time) }) }, last_time:function (time) { var $this = this var now = new Date(); $this.long = time.getTime() - now.getTime() $this.day = parseInt($this.long / (1000 * 60 * 60 * 24)) $this.hours = parseInt($this.long % (1000 * 60 * 60 * 24) / (1000 * 60 * 60)) $this.min = parseInt($this.long % (1000 * 60 * 60) / (1000 * 60)) $this.sec = parseInt($this.long % (1000 * 60) / 1000) $this.text = $this.day + "天" + $this.hours + "小时" + $this.min + "分钟" + $this.sec + "秒" }, pri:function (obj, time) { var $this = this return (function(time){ $this.last_time(time) $(obj).html($this.text) var temp = arguments.callee window.setTimeout(function () { temp(time) }, 1000) }(time)) } } cout.init(".time")
html
<input value="2012/5/7 18:00" type="text"> <input type="button" value="ok"> <div >按照上面格式输入时间</div>