この関数は、ページに費やした時間をdivに書き込む必要があります。 これはwindow.onbeforeunloadアラートでうまくいきました。素敵なワーキングアルゴリズムと作業axempleとして 私のonloadハンドラが動作しないのはなぜですか?
<script>
function tempo1() {
var d1 = new Date();
var t_day = d1.getDate();
var t_hour = d1.getHours();
var t_min = d1.getMinutes();
var t_sec = d1.getSeconds();
alert("Started at--> " + t_day + "days, " + t_hour + ":" + t_min + ":" + t_sec);
function tempo2() {
var d2 = new Date();
var t_day2 = d2.getDate();
var t_hour2 = d2.getHours();
var t_min2 = d2.getMinutes();
var t_sec2 = d2.getSeconds();
var day = t_day2 - t_day;
var hour = t_hour2 - t_hour;
var min = t_min2 - t_min;
var sec = t_sec2 - t_sec;
document.getElementById('timespent').innerHTML = "Total--> '+day+'days, '+hour+':'+min+':'+sec";
}
setInterval(tempo2(), 500);
}
window.onload = tempo1();
</script>
<div id="timespent">??days, ??:??:??</div>
:ライン
window.onload = tempo1();
で
<script>
startTime = new Date();
clockStart = startTime.getTime();
function initStopwatch() {
var NewTime = new Date();
return((NewTime.getTime() - clockStart)/1000);
}
function getSecs() {
var tSecs = Math.round(initStopwatch());
var iSecs = tSecs % 60;
var iMins = Math.round((tSecs-30)/60);
var sSecs ="" + ((iSecs > 9) ? iSecs : "0" + iSecs);
var sMins ="" + ((iMins > 9) ? iMins : "0" + iMins);
document.getElementById('timespent').innerHTML = sMins+":"+sSecs;
window.setTimeout(getSecs,1000);
}
window.onload=getSecs;
</script>
<div id=timespent></div>
絶対に – jony
でも、divの中には「Total - > '+ day +' days、 '+ hour +': '+ min +': '+ sec」しか表示されません。その実際の値が欠けている.. – jony
@ジョニー:それはコード内の無関係なエラーです、私は私の答えを更新しました。これらのことの最善の策は、 'x = foo();'の例のように、実際にコードを見て、それが何をしているのかを理解することです。 –