0
jQuery(document).ready(function() {
function updateTime() {
    $("#wtime").append('<p>Wacht nog: <?php echo $wait_time; ?></p> '); 
}

updateTime();
setInterval(updateTime, 1000); });

我想每秒更新一次 div #wtime 现在它只显示时间而不是更新它

4

2 回答 2

2

您必须将从服务器获取的时间解析为 javascriptDate()对象,然后每秒递增一次。

这是一个工作小提琴

// Make sure $wait_time has a valid format so that Date() can parse it.
// var serverTime  = new Date('<?php echo $wait_time; ?>');
var serverTime = new Date();

function updateTime() {
    /// Increment serverTime by 1 second and update the html for '#time'
    serverTime = new Date(serverTime.getTime() + 1000);
    $('#time').html(serverTime.toGMTString());
}

$(function() {
    updateTime();
    setInterval(updateTime, 1000);
});​

相关HTML:

<div id="wtime">
    <p>Wacht nog: <span id="time"></span></p>        
</div>​

希望能帮助到你。

于 2012-11-09T17:55:56.840 回答
0

我的小测试。

  <!DOCTYPE html>
  <head>
  <html> 
  <head> 
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
  <script> 
  $(document).ready(function(){
  function updateTime() {
  var currtime = new Date();
  var currtime1 = new Date(currtime.getTime());
  var currtime2 = new Date(currtime.toGMTString());
  var currtime3 = (currtime.getHours() + ":"+ currtime.getMinutes() + ":" + currtime.getSeconds());
  $('#1').html(currtime1);
  $('#2').html(currtime2);
  $('#3').html(currtime3);       
  }
  setInterval(updateTime, 1000); // 1000 miliseconds 
  }); 
  </script> 
  </head> 
  <body>
  <h1>
  <b> 
  <div id="1">The Current time is: </div>
  </br>
  <div id="2">The Current time is: </div>
  </br>
  <div id="3">The Current time is: </div>
  </b> 
  </h1> 
  </body> 
  </html>
于 2014-09-23T19:47:22.740 回答