基本上我目前有时钟显示时间,但倒计时不工作。我从这里获得了原始代码并进行了改编。
我要做的是让倒计时从我从数据库检索到的持续时间倒数。
下面是我如何检索我的持续时间://TRANSFORM MYSQL TIME INTO SECONDS
$duration_array = explode(':', $duration);
$length = ((int)$duration_array[0] * 3600) + ((int)$duration_array[1] * 60) + (int)$duration_array[2];
//CALCULATE FINISH TIME WITH START TIME AND DURATION.
$target_time = $length + time();
我正在解析这个到我的JavaScript倒计时函数成功,并转换成JavaScript日期()对象。
但是目前时钟没有倒数。
这是我的javascript:
function countdown_clock(target_time)
{
html_code = '<div id="countdown"></div>';
document.write(html_code);
var timetarget = new Date(target_time *1000);
countdown(timetarget);
}
function countdown(timetarget)
{
var Today = new Date();
Todays_Year = Today.getFullYear() - 2000;
Todays_Month = Today.getMonth();
//Convert both today's date and the target date into miliseconds.
Todays_Date = (new Date(Todays_Year, Todays_Month, Today.getDate(),Today.getHours(),Today.getMinutes(),Today.getSeconds())).getTime();
Target_Date = timetarget.getTime();
//Find their difference, and convert that into seconds.
Time_Left = Math.round((Target_Date - Todays_Date) / 1000);
if(Time_Left < 0)
Time_Left = 0;
var innerHTML = '';
//More datailed.
days = Math.floor(Time_Left / (60 * 60 * 24));
Time_Left %= (60 * 60 * 24);
hours = Math.floor(Time_Left / (60 * 60));
Time_Left %= (60 * 60);
minutes = Math.floor(Time_Left / 60);
Time_Left %= 60;
seconds = Time_Left;
if (seconds < 10){seconds = '0' + seconds;}
if (minutes < 10){minutes = '0' + minutes;}
innerHTML = hours + ':' + minutes + ':' + seconds + ' secs' ;
document.getElementById('countdown').innerHTML = innerHTML;
//Recursive call, keeps the clock ticking.
setTimeout('countdown('+timetarget+');', 1000);
}
我似乎真的很纠结于约会的事情,所以如果有任何帮助就太好了。
谢谢,
我认为问题是你的timetarget
变量正在失去它是Date
对象的事实。我修复了它并制作了一个jsFiddle,它似乎正在工作(倒计时)。
EDIT:另外,我认为Javascript可能已经搞砸了,因为你使用innerHTML
作为变量名,这是我做的另一个改变。
EDIT 2:这是一个稍微清理了一些代码的更新