如何function化倒计时?

我做了一个倒数计时器并使用HTML和CSS对其进行编码,但我无法使用jQuery使其正常运行。 我怎样才能将这个倒计时function化? 这是我更喜欢倒计时的HTML结构:

#

days

#

hours

#

minutes

#

seconds

如您所见,我已经添加了几天,几小时的段落元素,因此我不想通过jQuery嵌入任何额外的文本。

PS有谁能告诉我如何设置新的倒计时日期?

 function counter(count) { countdown = setInterval(function(){ var temp; $("p#d").html(Math.floor(count/(60*60*24))); temp = count%(60*60*24); $("p#h").html(Math.floor(temp/(60*60))); temp = count%(60*60); $("p#m").html(Math.floor(temp/(60))); temp = count%(60); $("p#s").html(temp); if (count == 0) { alert("time's up"); clearInterval(countdown); } count--; }, 1000); } counter(60*60*24*2); 

演示

编辑-1:

计数器获得时间秒。

 counter(10); //10 seconds counter(10*60) //600 seconds -> 10 minutes. counter(10*60*60) //36000 seconds -> 600 minutes -> 10 hour 

EDIT 2:

如果你想让它工作Date ,你应该改变这样的function,

 function counter(futureDate) { var today = new Date(); // today count = Math.floor((futureDate-today)/1000); countdown = setInterval(function(){ var temp; $("p#d").html(Math.floor(count/(60*60*24))); temp = count%(60*60*24); $("p#h").html(Math.floor(temp/(60*60))); temp = count%(60*60); $("p#m").html(Math.floor(temp/(60))); temp = count%(60); $("p#s").html(temp); if (count == 0) { alert("time's up"); clearInterval(countdown); } count--; }, 1000); } counter(new Date(2012,4,8)); // May 8, 2012 00:00:00 /* counter(new Date(2012,4,8,15,49,10)); // May 8, 2012 15:49:00 */​ 

http://jsfiddle.net/NfLAB/1/