我想知道是否有人可以帮助我。经过数小时的不懈搜索,在这里和网上我似乎找不到使用jquery的简单倒计时。我不想使用任何类型的插件,而只是简单的jquery代码来从日期倒数。我设法在下面找到此代码。但是即使将此代码放置在我的网站中,也不会显示任何内容。我从jquery.com添加了jquery文件,并添加了counterID不包含任何内容的正确div 。如果有人可以解释或告诉我如何在采用日期格式并返回倒数的函数中进行简单的倒数,我将不胜感激。
counter
var end = new Date('02/19/2012 10:1 AM'); var _second = 1000; var _minute = _second * 60; var _hour = _minute * 60; var _day = _hour * 24; var timer; function showRemaining() { var now = new Date(); var distance = end - now; if (distance < 0) { clearInterval(timer); document.getElementById('countdown').innerHTML = 'EXPIRED!'; return; } var days = Math.floor(distance / _day); var hours = Math.floor((distance % _day) / _hour); var minutes = Math.floor((distance % _hour) / _minute); var seconds = Math.floor((distance % _minute) / _second); document.getElementById('countdown').innerHTML = days + 'days '; document.getElementById('countdown').innerHTML += hours + 'hrs '; document.getElementById('countdown').innerHTML += minutes + 'mins '; document.getElementById('countdown').innerHTML += seconds + 'secs'; } timer = setInterval(showRemaining, 1000);
作为正常的javascript,它工作正常。
<script> var end = new Date('02/19/2012 10:1 AM'); var _second = 1000; var _minute = _second * 60; var _hour = _minute * 60; var _day = _hour * 24; var timer; function showRemaining() { var now = new Date(); var distance = end - now; if (distance < 0) { clearInterval(timer); document.getElementById('countdown').innerHTML = 'EXPIRED!'; return; } var days = Math.floor(distance / _day); var hours = Math.floor((distance % _day) / _hour); var minutes = Math.floor((distance % _hour) / _minute); var seconds = Math.floor((distance % _minute) / _second); document.getElementById('countdown').innerHTML = days + 'days '; document.getElementById('countdown').innerHTML += hours + 'hrs '; document.getElementById('countdown').innerHTML += minutes + 'mins '; document.getElementById('countdown').innerHTML += seconds + 'secs'; } timer = setInterval(showRemaining, 1000); </script> <div id="countdown"></div>
您的输出显示如下:-
<script> CountDownTimer('02/19/2012 10:1 AM', 'countdown'); CountDownTimer('02/20/2012 10:1 AM', 'newcountdown'); function CountDownTimer(dt, id) { var end = new Date(dt); var _second = 1000; var _minute = _second * 60; var _hour = _minute * 60; var _day = _hour * 24; var timer; function showRemaining() { var now = new Date(); var distance = end - now; if (distance < 0) { clearInterval(timer); document.getElementById(id).innerHTML = 'EXPIRED!'; return; } var days = Math.floor(distance / _day); var hours = Math.floor((distance % _day) / _hour); var minutes = Math.floor((distance % _hour) / _minute); var seconds = Math.floor((distance % _minute) / _second); document.getElementById(id).innerHTML = days + 'days '; document.getElementById(id).innerHTML += hours + 'hrs '; document.getElementById(id).innerHTML += minutes + 'mins '; document.getElementById(id).innerHTML += seconds + 'secs'; } timer = setInterval(showRemaining, 1000); } </script> <div id="countdown"></div> <div id="newcountdown"></div>
输出将如下所示:-