两种时间动态刷新方法

获取当前时间:一种是setTimeout,另一种为setInterval

 function timeFlash(){
        document.body.innerHTML="";
      var date = new Date();
        var year = date.getFullYear();
        var month = date.getMonth();
        var day = date.getDate();

        var hour = date.getHours();
        var minute = date.getMinutes();
        var second = date.getSeconds();

        var weekday = date.getDay();
        var n = "";
        switch (weekday){
            case 1:n=" 星期一";break;
            case 2:n=" 星期二";break;
            case 3:n=" 星期三";break;
            case 4:n=" 星期四";break;
            case 5:n=" 星期五";break;
            case 6:n=" 星期六";break;
            default :n=" 星期日";
        }
        var time = "现在时间为:"+year+"-"+month+"-"+day+" "+hour+":"+minute+":"+second+" "+n;
        document.write(time);
        setTimeout(function(){
            timeFlash()
        },1000);
    }
    setTimeout(function(){
        timeFlash()
    },1000);
    /*setInterval(function(){
        timeFlash()
    },1000);*/


你可能感兴趣的:(web前端)