AJAX/MySQL,JavaScript-数据库和倒计时


AJAX/MySQL,JavaScript - database and countdown

我通过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
            });
}
});
        });