js调用函数时括号加与不加的区别,function()&function

时间:2022-06-10 22:44:53
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>当前系统时间</title>
<link rel="stylesheet" href="style.css"  />
<script language="javascript" type="text/javascript">

  window.onload = function(){
    showTime();
  };
  function checkTime(i){  //补位处理
      if(i<10){
          i="0"+i;
      }
      else{return i;}

  }
  function showTime(){
    var now=new Date();
    var year=now.getFullYear();
    var month=now.getMonth();
    var day=now.getDate();
    var h=now.getHours();
    var m=now.getMinutes() ;
    var s= now.getSeconds() ;
    m=checkTime(m);
    s=checkTime(s);

    var weekday=new Array(7);
    weekday[0]="星期日";
    weekday[1]="星期一";
    weekday[2]="星期二";
    weekday[3]="星期三";
    weekday[4]="星期四";
    weekday[5]="星期五";
    weekday[6]="星期六";

    document.getElementById("show").innerHTML=""+year+"年"+month+"月"+day+"日 "+weekday[now.getDay()]+h+":"+m+":"+s;
    t=setTimeout('showTime()',1000);
    /*t=setTimeout(showTime,1000);也可以,同样效果*/
    /*t=setTimeout(showTime(),1000);不行,直接调用了函数的结果(也就是一个固定值),一开始就跑出了结果然后赋值给了t,相当于t=setTimeout(运行程序时的时间,1000)*/
  }

</script>
</head>
<body>
<div class="content1">
  <div id="show">显示时间的位置</div>
</div>
</body>
</html>