如何在加载页面后调用此函数(简单计时器)?我真的需要你最简单的方法来建议。
<script>
//Timer function
function timer(tag, sec) {
document.getElementById(tag).innerHTML = "<div id= 'inTime'>" +
(sec / 60 >> 0) + 'min ' + sec % 60 + 'sec' + '<br>' + "</div>";
if ((sec / 60 >> 0) != 0 || (sec % 60) != 0) {
setTimeout(function() {
timer(tag, sec);
}, 1000);
sec -= 1;
} else {
document.getElementById(tag).innerHTML = "Time is over!";
}
}
</script>
<div id="timerPlace"></div>
<br>
<br>
<br>
<br>
<!-- Write number of seconds here: onclick="timer('str',...here!...) -->
<button class="button" onclick="timer('timerPlace',3600); style.display = 'none'"> <span>Start Test</span>
</button>
<!-- Place this div where you whant timer to be. -->
&#13;
答案 0 :(得分:1)
你的问题不是那么清楚,但如果我得到了你的要求,你需要使用window.onload=function(){ /*code here*/ }
,一旦页面加载,代码就会被执行。
<script>
//Timer function
function timer(tag, sec) {
document.getElementById(tag).innerHTML = "<div id= 'inTime'>" +
(sec / 60 >> 0) + 'min ' + sec % 60 + 'sec' + '<br>' + "</div>";
if ((sec / 60 >> 0) != 0 || (sec % 60) != 0) {
setTimeout(function() {
timer(tag, sec);
}, 1000);
sec -= 1;
} else {
document.getElementById(tag).innerHTML = "Time is over!";
}
}
window.onload=function(){
timer('timerPlace',3600);
}
</script>
<div id="timerPlace"></div>
<br>
<br>
<br>
<br>
<!-- Write number of seconds here: onclick="timer('str',...here!...) -->
<button class="button" onclick="timer('timerPlace',3600); style.display = 'none'"> <span>Start Test</span>
</button>
<!-- Place this div where you whant timer to be. -->