私は自分のサイトにサーバー時間 (実行中) を表示する時計を持っていますが、動作を停止することがあります。これよりも良い方法があります:
PHP:
<?php print date("H:i", time()); ?>
Jクエリ:
<script type="text/javascript">
$(document).ready(function() {
function show_clock() {
$.ajax({
type: 'POST',
url: '/clock.php',
timeout: 1000,
success: function(data) {
$("#servertime").html(data);
window.setTimeout(show_clock, 1000);
},
});
}
show_clock();
});
</script>
編集:このコードも疲れていますが、完璧ではないようです:
<script type="text/javascript">
var currenttime = '<? print date("F d, Y H:i:s", time())?>' //PHP method of getting server date
var serverdate=new Date(currenttime)
function padlength(what){
var output=(what.toString().length==1)? "0"+what : what
return output
}
function displaytime(){
serverdate.setSeconds(serverdate.getSeconds()+1)
var timestring=padlength(serverdate.getHours())+":"+padlength(serverdate.getMinutes())
document.getElementById("servertime").innerHTML=timestring
}
setInterval("displaytime()", 1000)
</script>