您的位置:首页 > Web前端 > JavaScript

js中设置setInterval的注意点

2016-01-30 20:11 591 查看
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>

<script type="text/javascript" src="jquery-2.2.0.js"></script>
<script type="text/javascript" src="my.js"></script>
<script type="text/javascript">
// var len = $("img").length;
// console.log(len);
// $(function(){
//     var len = $("img").length;
//     console.log(len);
// })

</script>
</head>
<body>

<form action="tosubmit" method="post" class="yongle">
账号:<input type="text" name="mingzi" /><br>
密码:<input type="password" name="mima" /><br>
<input type="submit" value="登陆" />
</form>
<img /> <img />
<a href="dynamicMethod.do" class="yongle">转到dynamicMethod.jsp</a>
<a href="dynamicMethod">也是转到dynamicMethod.jsp</a>
<a href="dynamicMethod.action">哈哈,也是转到dynamicMethod.jsp</a>
时间:<input type="text" size=30 id='time'/>
</body>
<script type="text/javascript">
// var len = $("img").length;
// console.log(len);
// $(function(){
//     var len = $("img").length;
//     console.log(len);
// })
console.log(typeof $);
console.log($("img").length);
if(window.innerHeight){
alert('支持innerHeight');
}else {
alert('不支持innerHeight');
}
function currentTime(){
var d = new Date();
var curTime = d.toTimeString();
// $('#time').value=curTime;
$('#time').attr('value',curTime);
// document.getElementById('time').value=curTime;
}
window.setInterval("currentTime()",1000);//这个可行,时间会自动更新,注意点就使用window.setInterval(para1,para2)
//的第一个参数是一个function名,一定要用引号包裹起来,否则不会执行,而且浏览器不报错
window.setInterval(currentTime(),1000);//这个不可行,但是浏览器不会报错,
</script>
</html>


window.setInterval(para1,para2);

一共有以下几种形式:

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

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

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

3 :把第二种形式中para1的引号去掉,浏览器也不会报错,但是这个定时器不会正常工作,导致只是执行一次para1对应的具体的函数。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: