-2

私のコード

$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初心者です。結果が1行の場合、それは正常に動作しますが、結果が1行以上の場合、結果は最後の行とすべて同じですデータベース。どうすればこれを修正できますか?

これは私の出力です

0 Day 1 Hour 41 Min 25 Sec
0 Day 1 Hour 41 Min 25 Sec
4

2 に答える 2

0

すべてのカウンターに対して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()ます。エラーが発生した場合はお知らせください。修正を試みます。

于 2012-07-09T04:50:45.247 に答える
0

カウントダウンを表示している場合は、複数回ではなく 1 回である必要がある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);
于 2012-07-09T04:55:55.393 に答える