今天的用的时候,想隔一段时间之后运行一个方法,可是页面会报错,不是找不到对象,就是没有延迟效果、查到以下原因:
setTimeout有两种形式
setTimeout(code,interval)
setTimeout(func,interval,args)
其中code是一个字符串
func是一个函数.
注意"函数"的意义,是一个表达式,而不是一个语句.
比如你想周期性执行一个函数
function a(){
//...
}
可写为
setTimeout("a()",1000)
或
setTimeout(a,1000)
这里注意第二种形式中,是a,不要写成a(),切记!!!
?
以下是转载:
[setTimeout]
setTimeout(表达式,延时时间)
在执行时,是在载入后延迟指定时间后,去执行一次表达式,记住,次数是一次
用setTimeout实现的自动变化显示随机数的效果:
- <html>??
- <head>??
- <script>??
- window.onload=sett; ??
- function sett() ??
- { ??
- document.body.innerHTML=Math.random(); ??
- setTimeout("sett()",500); ??
- } ??
- </script>??
- </head>??
- <body>??
- </body>??
- </html>??
<html> <head> <script> window.onload=sett; function sett() { document.body.innerHTML=Math.random(); setTimeout("sett()",500); } </script> </head> <body> </body> </html>
[setInterval]
setInterval(表达式,交互时间)
则不一样,它从载入后,每隔指定的时间就执行一次表达式
用setInterval实现的自动变化显示随机数的效果:
- <html>??
- <head>??
- <script>??
- function sett() ??
- { ??
- document.body.innerHTML=Math.random(); ??
- } ??
- setInterval("sett();", 500); ??
- </script>??
- </script>??
- </head>??
- <body>??
- </body>??
- </html>??
<html> <head> <script> function sett() { document.body.innerHTML=Math.random(); } setInterval("sett();", 500); </script> </script> </head> <body> </body> </html>
在使用JScript的时候,我们有时需要间隔的执行一个方法,比如用来产生网页UI动画特效啥的。这是我们常常会使用方法setInterval或 setTimeout,但是由于这两个方法是由脚本宿主模拟出来的Timer线程,在通过其调用我们的方法是不能为其传递参数。
??? 我们常用的使用场景是:
- window.setTimeout("delayRun()", n); ??
- window.setInterval("intervalRun()", n); ??
- window.setTimeout(delayRun, n); ??
- window.setInterval(intervalRun, n);???
window.setTimeout("delayRun()", n); window.setInterval("intervalRun()", n); window.setTimeout(delayRun, n); window.setInterval(intervalRun, n);
??? 显然强行代参数的调用: window.setTimeout("delayRun(param)", n);
- window.setInterval("intervalRun(param)", n); ??
- window.setTimeout(delayRun(param), n); ??
- window.setInterval(intervalRun(param), n);???
window.setInterval("intervalRun(param)", n); window.setTimeout(delayRun(param), n); window.setInterval(intervalRun(param), n);
???? 都是错误的,因为string literals形式的方法调用,param必须是全局变量(即window对象上的变量)才行;而 function pointer形式的调用,完全错误了,这是把函数的返回值当成了setTimeout/setInterval函数的参数了,完全不是我们所望的事情。
??? 解决这个问题的办法可以使用匿名函数包装的方式,在以下scenario中我们这么做:
- function foo() ??
- { ??
- ????var param = 100; ??
- ???? window.setInterval(function() ??
- ???? { ??
- ???????? intervalRun(param); ??
- ???? }, 888); ??
- } ??
- function interalRun(times) ??
- { ??
- ????// todo: depend on times parameter ??
- }???
function foo() { var param = 100; window.setInterval(function() { intervalRun(param); }, 888); } function interalRun(times) { // todo: depend on times parameter }
??? 这样一来,就可以不再依赖于全局变量向delayRun/intervalRun函数中传递参数,毕竟当页面中的全局变量多了以后,会给脚本的开发、调试和管理等带来极大的puzzle。
setTimeout (表达式,延时时间)
setInterval(表达式,交互时间)
延时时间/交互时间是以豪秒为单位的(1000ms=1s)
setTimeout 在执行时,是在载入后延迟指定时间后,去执行一次表达式,仅执行一次
setInterval在执行时,它从载入后,每隔指定的时间就执行一次表达式
1,基本用法:
?? 执行一段代码:
?? var i=0;
?? setTimeout("i+=1;alert(i)",1000);
?? 执行一个函数:
?? var i=0;
?? setTimeout(function(){i+=1;alert(i);},1000);
??
?? //注意比较上面的两种方法的不同。
?? 下面再来一个执行函数的:
?? var i=0;
?? function test(){
?????? i+=1;
?????? alert(i);
?? }
?? setTimeout("test()",1000);
?? 也可以这样:
?? setTimeout(test,1000);
?? 总结:
?? setTimeout的原型是这样的:
?? iTimerID = window.setTimeout(vCode, iMilliSeconds [, sLanguage])
??
setTimeout有两种形式
setTimeout(code,interval)
setTimeout(func,interval,args)
其中code是一个字符串
func是一个函数.
注意"函数"的意义,是一个表达式,而不是一个语句.
比如你想周期性执行一个函数
function a(){
????? //...
}
可写为
setTimeout("a()",1000)
或
setTimeout(a,1000)
这里注意第二种形式中,是a,不要写成a(),切记!!!
展开来说,不管你这里写的是什么,如果是一个变量,一定是一个指向某函数的变量;如果是个函数,那它的返回值就 要是个函数
2,用setTimeout实现setInterval的功能
??? 思路很简单,就是在一个函数中调用不停执行自己,有点像递归
??? var i=0;
??? function xilou(){
??????? i+=1;
??????? if(i>10){alert(i);return;}
??????? setTimeout("xilou()",1000);
??????? //用这个也可以
??????? //setTimeout(xilou,1000);
??? }
???
??? 3,在类中使用setTimeout
??? 终于到正题了,其实在类中使用大家遇到的问题都是关于this的,只要解决了这个this的问题就万事无忧了。
呵呵。让我们来分析一下:
???
??? function xilou(){
??????? this.name="xilou";
??????? this.sex="男";
??????? this.num=0;
??? }
??? xilou.prototype.count=function(){
??????? this.num+=1;
??????? alert(this.num);
??????? if(this.num>10){return;}
??????? //下面用四种方法测试,一个一个轮流测试。
??????? setTimeout("this.count()",1000);//A:当下面的x.count()调用时会发生错误:对象不支持此属性或方法。
??????? setTimeout("count()",1000);//B:错误显示:缺少对象
??????? setTimeout(count,1000);//C:错误显示:'count'未定义
??????? //下面是第四种
??????? var self=this;
??????? setTimeout(function(){self.count();},1000);//D:正确
???????
??? }
???
??? var x=new xilou();
??? x.count();
???
??? 错误分析:
??? A:中的this其实指是window对象,并不是指当前实例对象
??? B:和C:中的count()和count其实指的是单独的一个名为count()的函数,但也可以是window.count(),因为window.count()可以省略为count()
??? D:将变量self指向当前实例对象,这样js解析引擎就不会混肴this指的是谁了。
???
??? 话说回来,虽然我们知道setTimeout("this.count()",1000)中的this指的是window对象,但还是不明白为什么会是
??? window对象^_^(有点头晕...)
??? 那我们可以想象一下这个setTimeout是怎样被定义的:
??? setTimeout是window的一个方法,全称是这样的:window.setTimeout()
??? 那应该是这样被定义的:
??? window.setTimeout=function(vCode, iMilliSeconds [, sLanguage]){
??????? //.....代码
??????? return timer//返回一个标记符
??? }
??? 所以当向setTimeout()传入this的时候,当然指的是它所属的当前对象window了。
?
