问答详情
源自:7-3 返回/设置年份方法

为什么加一个循环让它一直显示时间就卡着运行不了了啊?


<html>


<head>

<title>计时器</title>

<script type="text/javascript">


var mydate=new Date(); 


var myyear=mydate.getFullYear();


var mymonth=mydate.getMonth()+1;


var myday=mydate.getDate();


var myhour=mydate.getHours();


var myminute=mydate.getMinutes();


var mysecond=mydate.getSeconds();


while(1){

document.write(myyear+"年"+mymonth+"月"+myday+"日"+myhour+"时"+myminute+"分"+mysecond+"秒");


}

</script>


</head>


<body>


</body>


</html>


提问者:四夕云遗 2018-07-09 16:26

个回答

  • qq_苦笑释怀_0
    2018-07-09 17:06:50
    已采纳

    你需要循环的是new Date();


  • qq_苦笑释怀_0
    2018-07-16 19:51:10

    function ChangeTime(){		
        var mydate=new Date(); 		
        var myyear=mydate.getFullYear();		
        var mymonth=mydate.getMonth()+1;		
        var myday=mydate.getDate();		
        var myhour=mydate.getHours();		
        var myminute=mydate.getMinutes();		
        var mysecond=mydate.getSeconds();		
        //document.write(myyear+"年"+mymonth+"月"+myday+"日"+myhour+"时"+myminute+"分"+mysecond+"秒");
        console.log(myyear+"年"+mymonth+"月"+myday+"日"+myhour+"时"+myminute+"分"+mysecond+"秒");	
    }		
    setInterval(ChangeTime,1000);
    
    F12开发者工具里面看变化


  • 四夕云遗
    2018-07-09 18:32:15

    请问你说的是这样吗?

    while(1){

    var mydate=new Date(); 

    var myyear=mydate.getFullYear();

    var mymonth=mydate.getMonth()+1;

    var myday=mydate.getDate();

    var myhour=mydate.getHours();

    var myminute=mydate.getMinutes();

    var mysecond=mydate.getSeconds();

    document.write(mydate);

    document.write(myyear+"年"+mymonth+"月"+myday+"日"+myhour+"时"+myminute+"分"+mysecond+"秒");

    }