实时倒计时钟的问题
Problem with live countdown clock
基本上我目前有时钟显示时间,但倒计时不工作。我从这里获得了原始代码并进行了改编。
我要做的是让倒计时从我从数据库检索到的持续时间倒数。
下面是我如何检索我的持续时间://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:这是一个稍微清理了一些代码的更新
相关文章:
- 在指令控制器中使用$attrs时出现问题
- 将PHP变量传递给jQuery时遇到问题
- Canvas Html5绘图应用程序,移动画布会导致重大问题
- 参数变量出现ngTable指令问题
- 剑道网格jQuery动画()问题
- 我的jQuery插件参数没有正确启动,遇到了问题
- Phonegap-(安卓/iphone)多个图像的图像库出现问题
- TableExport jquery插件:文件名和扩展名问题
- JavaScript Pub/Sub属性访问问题
- JavaScript异步问题
- 如何解决Yii中的页面刷新问题
- Safari(Mac OS)上的jQuery平滑滚动问题
- jqGrid树网格问题
- 使用正则表达式评估电子邮件地址时出现性能问题
- 如何消除代码中的未定义和其他问题
- JavaScript代码问题:我正在将对象转换为数组
- 三星智能电视应用程序;Brightcove示例应用程序远程控制问题
- 由于iframe导致的问题
- 在我的倒计时钟上用两位数显示小于10的数字时遇到了问题
- 实时倒计时钟的问题