2016-03-08 46 views
2

我正試圖在本網站上顯示日期和時間,現在您必須刷新頁面以便更新(秒等)。我如何自動更新秒數?自動更新秒數?

<!doctype html> 
<html lang="en"> 
    <head> 
     <meta charset="utf-8"> 
     <title>JavaScript Testbed</title> 
    </head> 
    <body> 
     <h1> 
      Javascript will find out the date and the browser. 
     </h1> 
     <script type="text/javascript"> 
      var currentTime = new Date(); 
      var hours = currentTime.getHours(); 
      var minutes = currentTime.getMinutes(); 
      var seconds = currentTime.getSeconds(); 
      var suffix = "AM"; 

      if (seconds < 10) 
       seconds = "0" + seconds; 

      if (minutes < 10) 
       minutes = "0" + minutes; 

      if (hours >= 12) { 
       suffix = "PM"; 
       hours = hours - 12; 
      } 
      if (hours == 0) { 
       hours = 12; 
      } 

      document.write("<b><div id='d1'>" + hours + ":" + 
       minutes + ":"+ seconds + " " + suffix + "</div></b>"); 
     </script> 
    </body> 
</html> 
+0

使用'setInterval'週期性的調用處理程序... – Rayon

回答

0

這適用於我的網站。使用setInterval功能

$(document).ready(function(){ 

    set_time() 
    window.setInterval(function(){set_time()}, 1000) 

}); 


function set_time(){ 

    time = $('#time') 
    raw_time = new Date(); 
    real_time = raw_time.getFullYear() + "-" + (raw_time.getMonth() + 1) + "-" + raw_time.getDate() + " " + (raw_time.getHours()-12) + ":" + raw_time.getMinutes() + ":" + raw_time.getSeconds(); 
    time.html(real_time) 
} 
+0

,如果你不想使用jQuery的你只會使用document.getElementById而不是$('#') –

2

裹在你的功能更新邏輯,並調用它每1000毫秒:)

function updateTime() { 
    var currentTime = new Date(); 
    var hours = currentTime.getHours(); 
    var minutes = currentTime.getMinutes(); 
    var seconds = currentTime.getSeconds(); 
    var suffix = "AM"; 

    if (seconds < 10) 
     seconds = "0" + seconds; 

    if (minutes < 10) 
     minutes = "0" + minutes; 

    if (hours >= 12) { 
     suffix = "PM"; 
     hours = hours - 12; 
    } 
    if (hours == 0) { 
     hours = 12; 
    } 

    document.write("<b><div id='d1'>" + hours + ":" + 
     minutes + ":"+ seconds + " " + suffix + "</div></b>"); 
} 

window.setInterval(updateTime, 1000); 
1

HTML

<span class="clock"></span> 

的Javascript

jQuery(function($) { 
    setInterval(function() { 
    var date = new Date(), 
     time = date.toLocaleTimeString(); 
    $(".clock").html(time); 
    }, 1000); 
}); 
0

使用純JavaScript和更新<span>元素

setInterval(function() { 
 
    var currentTime = new Date(); 
 
    var hours = currentTime.getHours(); 
 
    var minutes = currentTime.getMinutes(); 
 
    var seconds = currentTime.getSeconds(); 
 
    var suffix = "AM"; 
 

 
    if (seconds < 10) 
 
    seconds = "0" + seconds; 
 

 
    if (minutes < 10) 
 
    minutes = "0" + minutes; 
 

 
    if (hours >= 12) { 
 
    suffix = "PM"; 
 
    hours = hours - 12; 
 
    } 
 
    if (hours == 0) { 
 
    hours = 12; 
 
    } 
 

 
    document.getElementById("clock").innerHTML = "<b><div id='d1'>" + hours + ":" + 
 
    minutes + ":" + seconds + " " + suffix + "</div></b>"; 
 
}, 1000);
<span id="clock"></span>