我的代码
$sql="SELECT * FROM `room_lists`";
$sqlquery = mysql_query($sql);
while($result = mysql_fetch_array($sqlquery)){
$toDay = strtotime($result['timeStop']);
//echo $toDay;
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=tis620" />
<script type="text/javascript" src="core/jquery-latest.js"></script>
<title>test</title>
<script type="text/javascript">
function countDown(times){
var toDay=Math.round(new Date().getTime()/1000);
var difTime=times-toDay;
var day=0,hours=0,minutes=0,seconds=0;
if(difTime>0){
day=Math.floor(difTime/84600);
hours=Math.floor((difTime/3600))%24;
minutes=Math.floor(difTime/60)%60;
seconds=Math.floor(difTime)%60;
countDown_onLoad();
}
else{
alert('asd'); //time out
}
$('.show').html(day+' Day '+hours+' Hour '+minutes+' Min '+seconds+' Sec ');
}
function countDown_onLoad(){
setTimeout("countDown(<?=$toDay ?>);",1000);
}
$(document).ready(function() {
countDown_onLoad();
});
</script>
</head>
<body>
<div class="show"></div>
</body>
</html>
<? } ?>
你好,我是一个使用javascript的新手。我创建了倒计时计时器,像拍卖网站一样使用js函数,用php查询从数据库(时间戳类型)调用,如果结果是1行,它可以正常工作,但如果超过1行,结果都和数据库中的最后一行相同。我该怎么解决这个问题?
这是我的输出
0 Day 1 Hour 41 Min 25 Sec
0 Day 1 Hour 41 Min 25 Sec
您似乎正在为每个计数器输出一个完整的HTML文档(减去打开的<html>
标记和<!doctype>
)。
核心问题是,您使用$(".show").html(...)
来输出计数器,这本质上是告诉它将该内容放在每个.show
元素中。最终结果是,它们都显示出相同的东西:无论最后一个计数器在哪里。
你的方法几乎无法解决,但这是我最好的尝试:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=tis620" />
<script type="text/javascript" src="core/jquery-latest.js"></script>
<title>test</title>
<script type="text/javascript">
function countDown(){
var toDay=Math.round(new Date().getTime()/1000);
$(".show").each(function() {
var elm = $(this);
var difTime=this.timestamp-toDay;
var day=0,hours=0,minutes=0,seconds=0;
if(difTime>0){
day=Math.floor(difTime/84600);
hours=Math.floor((difTime/3600))%24;
minutes=Math.floor(difTime/60)%60;
seconds=Math.floor(difTime)%60;
}
else{
alert('asd'); //time out
elm.removeClass("show");
}
elm.html(day+' Day '+hours+' Hour '+minutes+' Min '+seconds+' Sec ');
});
}
function countDown_onLoad(){
$(".show").each(function() {
this.timestamp = parseInt(this.firstChild.nodeValue,10);
});
setInterval(countDown,1000);
}
$(document).ready(function() {
countDown_onLoad();
});
</script>
</head>
<body>
<?php
$sql="SELECT * FROM `room_lists`";
$sqlquery = mysql_query($sql);
while($result = mysql_fetch_array($sqlquery)){
$toDay = strtotime($result['timeStop']);
echo "<div class='"show'">".$toDay."</div>";
}
?>
</body>
</html>
请注意,我不使用jQuery。我之所以在这里使用它,是因为你已经在使用了。考虑到这一点,我可能错误地使用了.each()
。如果你遇到任何错误,请告诉我,我会尽力修复它们。
如果显示倒计时,则必须有一次,而不是多次,因此在检索值时首先设置LIMIT 1
而不是做这个
function countDown(times){
// blah blah blah
}
function countDown_onLoad(){
setTimeout("countDown(<?=$toDay ?>);",1000);
}
$(document).ready(function() {
countDown_onLoad();
});
尝试以下方法
var countdown = function(myTime)
{
// blah blah blah
}
window.setInterval(countdown(<?=$toDay?>), 1000);