js中设置setInterval的注意点

 1 <html>
 2 <head>
 3 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 4 <title>Insert title here</title>
 5 
 6 <script type="text/javascript" src="jquery-2.2.0.js"></script>
 7 <script type="text/javascript" src="my.js"></script>
 8 <script type="text/javascript">
 9     // var len = $("img").length;
10     // console.log(len);
11     // $(function(){
12     //     var len = $("img").length;
13     //     console.log(len);    
14     // })
15     
16 </script>
17 </head>
18 <body>
19 
20     <form action="tosubmit" method="post" class="yongle">
21         账号:<input type="text" name="mingzi" /><br>
22         密码:<input type="password" name="mima" /><br> 
23         <input type="submit" value="登陆" />
24     </form>
25     <img /> <img />
26     <a href="dynamicMethod.do" class="yongle">转到dynamicMethod.jsp</a>
27     <a href="dynamicMethod">也是转到dynamicMethod.jsp</a>
28     <a href="dynamicMethod.action">哈哈,也是转到dynamicMethod.jsp</a>
29     时间:<input type="text" size=30 id='time'/>
30 </body>
31 <script type="text/javascript">
32     // var len = $("img").length;
33     // console.log(len);
34     // $(function(){
35     //     var len = $("img").length;
36     //     console.log(len);    
37     // })
38     console.log(typeof $);
39     console.log($("img").length);
40     if(window.innerHeight){
41         alert('支持innerHeight');
42     }else {
43         alert('不支持innerHeight');
44     }
45     function currentTime(){
46         var d = new Date();
47         var curTime = d.toTimeString();
48         // $('#time').value=curTime;
49         $('#time').attr('value',curTime);
50         // document.getElementById('time').value=curTime;
51     }
52     window.setInterval("currentTime()",1000);//这个可行,时间会自动更新,注意点就使用window.setInterval(para1,para2)
53                 //的第一个参数是一个function名,一定要用引号包裹起来,否则不会执行,而且浏览器不报错
54     window.setInterval(currentTime(),1000);//这个不可行,但是浏览器不会报错,
55 </script>
56 </html>

 window.setInterval(para1,para2);

一共有以下几种形式:

1 window.setInterval(function(){alert('xxx')},1000); para1为匿名函数的形式,

2 window.setInterval("myFunc()",1000);  para1为一个字符串,而且这个字符串是一个已经写好的函数的名称。

以上这两种可以正常运行,

3 :把第二种形式中para1的引号去掉,浏览器也不会报错,但是这个定时器不会正常工作,导致只是执行一次para1对应的具体的函数。

原文地址:https://www.cnblogs.com/Sunnor/p/5171999.html