1

我正在使用以下代码在我的网页上显示日期。我需要每分钟更新一次。怎么做?

var d=new Date();
var n=d.toString();
document.write(n);

目前它的静态,意味着当页面加载时,显示那一刻的日期时间。我必须每分钟更新一次时间而不刷新页面。

4

4 回答 4

5

尝试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();
        });

您可以使用document.getElementById("my_box1").innerHTML=time;而不是$('#my_box1')

来自 MDN:

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.

于 2013-02-18T06:37:25.900 回答
1

这是每秒打印日期时间的方法

函数显示日期(){

    var n=BuildDateString();
          document.write(n);
    window.setTimeout("displayDate();", 1000); // to print it every minute take 1000*60 
}

function BuildDateString()
{
    var today = new Date()
    var year = today.getYear()
    if (year < 2000)
        year = "19" + year
    var _day = today.getDate()
    if (_day < 10)
        _day = "0" + _day
    var _month = today.getMonth() + 1
    if (_month < 10)
        _month = "0" + _month
    var hours = today.getHours()
    var minutes = today.getMinutes()
    var seconds = today.getSeconds()
    var dn = "AM" 
    if (hours > 12)
    {
        dn = "PM"
        hours = hours - 12
    }
    if (hours == 0)
        hours = 12
    if (minutes < 10)
        minutes = "0" + minutes
    if (seconds < 10)
        seconds = "0" + seconds
    var DateString = _month+"/"+_day+"/"+year+" "+hours+":"+minutes+":"+seconds+" "+dn

    return DateString;
}
于 2013-02-18T06:34:22.097 回答
1

我正在使用以下方法:

var myVar=setInterval(function(){myDateTimer()},60000);

            function makeArray() 
            {
                for (i = 0; i<makeArray.arguments.length; i++)
                this[i + 1] = makeArray.arguments[i];
            }

            function myDateTimer()
            {
                var months = new makeArray('January','February','March','April','May',
                'June','July','August','September','October','November','December');
                var days = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'];
                var date = new Date();
                var day = date.getDate();
                var month = date.getMonth() + 1;
                var yy = date.getYear();
                var year = (yy < 1000) ? yy + 1900 : yy;
                var hours = date.getHours();
                var minutes = date.getMinutes();
                var finaldate = days[ date.getDay() ] + ", " + months[month] + " " + day + ", " + year + " " + hours +" : " + minutes;
                document.getElementById("showDateTime").innerHTML=finaldate;
            }
于 2013-02-18T06:38:37.260 回答
0

就这样做

$(function(){
setInterval(function(){
    var d=new Date();
    var n=d.toString();
    $('#test').html(n);
},1000);
});

演示http://runjs.cn/code/txlexzuc

于 2013-02-18T06:50:06.573 回答