日期:2014-05-16  浏览次数:20443 次

setTimeout函数执行 某个对象本身的方法函数,怎么写也不对,救急。
<script   type= "text/javascript "   language= "javascript ">
function   A(){

}

A.prototype.b=function(str){
alert( "方法b的语句: "+str);
}

A.prototype.c=function(str){
this.b( "我要延迟执行该方法 ");

setTimeout( 'this.b( '+str+ ') ',1000);//怎么写也不对
setTimeout( "this.b( "+str+ ") ",1000);//怎么写也不对
setTimeout(this. 'b( '+str+ ') ',1000);//怎么写也不对
setTimeout(this. "b( "+str+ ") ",1000);//怎么写也不对
setTimeout(this.b( " "+str+ " "),1000);//怎么写也不对

alert( "方法c的语句: "+str);
}

var   x=new   A();
x.c( "字符 ");

</script>


------解决方案--------------------
setTimeout( "this.b( '+str+ ') ",1000);//这个在执行的时候,this是window的引用。
所以如果想正确执行,就要把this换成对象x,或者想楼上那么做也可以。
------解决方案--------------------
二楼的是正确的,建立一个闭包就行了.

------解决方案--------------------
setTimeout Method Internet Development Index

--------------------------------------------

Evaluates an expression after a specified number of milliseconds has elapsed.

Syntax

iTimerID = window.setTimeout(vCode, iMilliSeconds [, sLanguage])
Parameters

vCode Required. Variant that specifies the function pointer or string that indicates the code to be executed when the specified interval has elapsed.
iMilliSeconds Required. Integerthat specifies the number of milliseconds.
sLanguage Optional. Stringthat specifies one of the following values: JScript Language is JScript.
VBScript Language is VBScript.
JavaScript Language is JavaScript.


5555555555 我没注意 还一直以为settimeout的第一个参数只能是string
看了2楼的回复再看说明才发现 也可以是 function的变量指针
看来原来的程序要大改了 就是因为以为只能是string搞得原来的程序好复杂