我正在使用以下代码在我的网页上显示日期.我需要每分钟更新一次.怎么做?
var d=new Date();
var n=d.toString();
document.write(n);
Run Code Online (Sandbox Code Playgroud)
目前它的静态意味着当页面加载时,显示该时刻的日期时间.我必须每分钟更新一次,而不刷新页面.
setInterval():http://jsfiddle.net/4vQ8C/ var nIntervId; //<----make a global var in you want to stop the timer
//-----with clearInterval(nIntervId);
function updateTime() {
nIntervId = setInterval(flashTime, 1000*60); //<---prints the time
} //----after every minute
function flashTime() {
var now = new Date();
var h = now.getHours();
var m = now.getMinutes();
var s = now.getSeconds();
var time = h + ' : ' + m + ' : ' + s;
$('#my_box1').html(time); //<----updates the time in the $('#my_box1') [needs jQuery]
}
$(function() {
updateTime();
});
Run Code Online (Sandbox Code Playgroud)
你可以用document.getElementById("my_box1").innerHTML=time;而不是$('#my_box1')
About setInterval : --->Calls a function or executes a code snippet repeatedly, with a fixed time delay between each call to that function.
About setTimeout : ----> Calls a function or executes a code snippet after specified delay.
| 归档时间: |
|
| 查看次数: |
5435 次 |
| 最近记录: |