我一直在为我自己的服装Javascript倒计时工作,在网站上进行多次倒计时,但日期计算不正确。
我有这个javascript用于显示时间计数器:
$(function(){
$('.countdown').each(function() {
var $this = $(this),
ts = new Date($this.data('ts')),
newYear = true;
if((new Date()) > ts){
newYear = false;
}
$this.countdown({
timestamp : ts,
callback : function(days, hours, minutes, seconds){
var message = "";
message += days + " hari" + ( days==1 ? '':'' ) + ", ";
message += hours + " jam" + ( hours==1 ? '':'' ) + ", ";
message += minutes + " menit" + ( minutes==1 ? '':'' ) + ", ";
message += seconds + " detik" + ( seconds==1 ? '':'' ) + " <br />";
$this.next().html(message);
}
});
});
});
这就是我的表格:
id | tgl_close1 | idrek
1 | 2014-11-25 08:00:00 | 1
2 | 2014-11-26 10:00:00 | 1
3 | 2014-11-26 12:10:00 | 1
我将倒计时乘以foreach但它总是显示'NaN'而不是数字:
<?php
$fetch = mysql_query("select tgl_close1
from tba
where idrek = 1");
/* Retrieve and store in array the results of the query.*/
while ($row = mysql_fetch_array($fetch, MYSQL_ASSOC)) {
$tgl_close1[] = date("Y, n-1, j, G, i, s", strtotime($row['tgl_close1']));
}
foreach ($tgl_close1 as $tglclose){
?>
<br>
<table border="0"><tr><td>
<div class="countdown" data-ts="<?php echo $tglclose"></div>
<p class="note"></p>
</td></tr></table>
<?php
}
}
mysql_close($conn);
?>
<script type="text/javascript">
$(function(){
$('.countdown').each(function() {
var $this = $(this),
ts = new Date($this.data('ts')),
newYear = true;
if((new Date()) > ts){
newYear = false;
}
$this.countdown({
timestamp : ts,
callback : function(days, hours, minutes, seconds){
var message = "";
message += days + " hari" + ( days==1 ? '':'' ) + ", ";
message += hours + " jam" + ( hours==1 ? '':'' ) + ", ";
message += minutes + " menit" + ( minutes==1 ? '':'' ) + ", ";
message += seconds + " detik" + ( seconds==1 ? '':'' ) + " <br />";
$this.next().html(message);
}
});
});
});
</script>
有人能告诉我我的代码有什么问题吗?
答案 0 :(得分:0)
好吧,我将我的javascript更改为:
$(function(){
$('.countdown').each(function() {
var $this = $(this),
ts = new Date($('.note').data('ts')),
newYear = true;
if((new Date()) > ts){
newYear = false;
}
$this.countdown({
timestamp : ts,
callback : function(days, hours, minutes, seconds){
var message = "";
message += days + " hari" + ( days==1 ? '':'' ) + ", ";
message += hours + " jam" + ( hours==1 ? '':'' ) + ", ";
message += minutes + " menit" + ( minutes==1 ? '':'' ) + ", ";
message += seconds + " detik" + ( seconds==1 ? '':'' ) + " <br />";
$this.next().html(message);
}
});
});
});
它将日期传递给javascript,但传递到javascript的$tglclose
只是第一行,所以我有三个计数器和相同的时间计数器..