我正在通过 Ajax 显示数据库中的数据,并且需要添加包含数据库中时间的倒计时器。我已经挣扎了大约24小时,有可能做到吗?你可以在这里查看主要代码 http://www.egrupper.pl正如您将看到的,倒计时器不起作用。 AJAX代码:
function showDeals(sid,limit,cat)
{
sid=typeof sid !== 'undefined' ? a : 1;
limit = typeof limit !== 'undefined' ? limit : 0.7;
if(sid=="" || limit==""){
document.getElementById("con").innerHTML="";
return;
}
if(window.XMLHttpRequest){
xmlhttp=new XMLHttpRequest();
}else{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4 && xmlhttp.status==200){
document.getElementById("con").innerHTML=xmlhttp.responseText;
$('#con').masonry({
itemSelector: '.clsDeal_Blk_Cont'
});
}
}
xmlhttp.open("GET","getdeals.php?sid="+sid+"&limit="+limit+"&cat="+cat,true);
xmlhttp.send();
}
在 getdeals.php 中我有倒数计时器代码,如下所示:
$(document).ready(function(){
$("#countdown_<?php echo $id; ?>").countdown({
date: "date_from_database",
format: "on"
},
function() {
// callback function
});
});
感谢@user2008945的帮助,这对我很有用,但无论如何我在页面上需要多个倒计时器,我虽然这样的事情可以,但不幸的是不行:
success:function(rows){
for (var i in rows){
var row=rows[i];
var id=row[0];
var end_date=row[1];
$("#countdown_"+id).countdown({
date: end_date,
format: "on"
},
function() {
// callback function
});
}
}
和
$data=array();
while($row=mysql_fetch_row($result))
{
$data[]=$row;
}
die (json_encode($data));
最佳答案
除非您熟悉 jquery ajax,否则这段代码不会有太大帮助, 但如果您决定使用 jquery ajax,那么您可以使用以下代码:
$(document).ready(function(){
$.ajax({
url:"getdeals.php?sid="+sid+"&limit="+limit+"&cat="+cat,
type:"GET",
dataType:'json',
success:function(data){
//data contains value like {countDownId:'someid',countDownDate:'somedate'},
//its in json format,
// in your getdeals.php write some thing like this
// die (json_encode(array('countDownId'=>'someid','countDownDate'=>'somedate')));
$("#countdown_"+data.countDownId).countdown({
date: data.countDownDate,
format: "on"
},
function() {
// callback function
});
}
});
});
关于php - AJAX/MySQL、JavaScript - 数据库和倒计时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14760321/