php+jsリアルタイム表示時間の問題
1988 ワード
まずこのコードを見てみましょう
このコードはどこが問題があって、どうしてホームページがロードした後にホームページはずっとその時間が変わらないのですか?phpとjsのロードメカニズムは何が違いますか?
これは間違っています.phpコードは出力前に解釈されます.ブラウザが受け取ったコードはこのようなものです.
これにより、定期的に更新された値が次のように変更されます.
$(function(){
setInterval(function(){
var timer = '';
$("#test").append(timer);
},500);
});
このコードはどこが問題があって、どうしてホームページがロードした後にホームページはずっとその時間が変わらないのですか?phpとjsのロードメカニズムは何が違いますか?
これは間違っています.phpコードは出力前に解釈されます.ブラウザが受け取ったコードはこのようなものです.
$(function(){
setInterval(function(){
var timer = '2015-01-02 12:02:02';
$("#test").append(timer);
},500);
});
これにより、定期的に更新された値が次のように変更されます.
// Date , Date String
// (M)、 (d)、 (h)、 (m)、 (s)、 (q) 1-2 ,
// (y) 1-4 , (S) 1 ( 1-3 )
// :
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function (fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //
"d+": this.getDate(), //
"h+": this.getHours(), //
"m+": this.getMinutes(), //
"s+": this.getSeconds(), //
"q+": Math.floor((this.getMonth() + 3) / 3), //
"S": this.getMilliseconds() //
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
$(function(){
setInterval(function(){
var timer = new Date().Format("yyyy-MM-dd HH:mm:ss");
$("#test").append(timer);
},500);
});