setInterval('this.show()',this.DelayTime);
解决方案 »
- 关于一个递归算法,我要求他被调用了多少次。
- Jquery为DIV添加点击事件,怎么弄。
- 【求助】javscript空格符问题
- 请问在js中的/u打头的中文编码是什么编码?如何转码?
- 求一个在点击按钮让iframe中的form提交的js,要求FireFox可用
- 超链接加了onclick属性,调用包含confirm的函数,不管点确定还是取消都无视confirm
- ~~~~~~~~~~~~jquery如何判断table当前行第一列是否是radio,如果是就选中!~~~~~~~~~~~~~~~~~~~
- 请问如何通过IE调用一个本地的可执行文件(*.exe)?
- 滚动问题!!!!!!
- 能用javascript做成这种按扭吗?(急需。。)
- javascript调用函数的时候提示缺少对象,代码如下
- 很着急,JS怎么计算周岁
这样写的话会显示对象不支持此属性
如果我写成这样的话
setInterval( 'show.show() ',this.DelayTime);那声明的时候就必须是
var show=new showImg();
必须和setInterval( 'show.show() ',this.DelayTime);中的show.show()相同
这样写的话那不是起不到自定义类的作用吗,没有实际意义,
而且var show=new showImg();还必须定义为全局的期待楼下
function test(){
this.f1="";
this.DelayTime=1000;
}
test.prototype.show=function(){
this.f1 = "fdsafsda";
return this.f1;};
test.prototype.play = function(){
alert(this.show());
};这样你就能看出来了 是调用了 Show函数
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<script type="text/javascript">
function test(){
this.str="test";
this.DelayTime=1000;
}
test.prototype.show=function(){
alert(this.str);};
test.prototype.play = function(){
setInterval("this.show()",this.DelayTime);
};
window.onload=function(){
var test=new test();
test.play();
}
</script>
</head>
<body>
</body>
</html>
大家再看看
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<script type="text/javascript">
function test(){
this.str="test";
this.DelayTime=1000;
}
test.prototype.show=function(){
alert(this.str);
};
test.prototype.play = function(){
setInterval('this.show()',this.DelayTime);
//setInterval('test.show()',this.DelayTime);
};
</script>
<script type="text/javascript">
var test=new test();
test.play();
</script>
</head>
<body>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<script type="text/javascript">
function Test(){
this.str="test";
this.DelayTime=1000;
}
Test.show=function(str){//改为静态的方法
alert(str);
};
Test.prototype.play = function(){
//我个人认为可能是因为setInterval是window对象的方法,setInterval要执行的函数必须是window域内的
//此时this指向的是window,而非Test的实例方法
setInterval('Test.show("'+this.str+'")',this.DelayTime);//调用静态的方法
};
</script>
<script type="text/javascript">
var test=new Test();
test.play();
</script>
</head>
<body>
</body>
</html>
setInterval(this.show,this.DelayTime); var a=new Test();
a.play();
调试没问题
看来碰到这样情况只有用静态的,但使用静态的心里感觉太不爽了楼上再看看<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>test</title>
<script type="text/javascript">
window.str="test2";
function test(){
this.str="test";
this.DelayTime=1000;
}
test.prototype.show=function(){
alert(this.str);
};
test.prototype.play = function(){
setInterval(this.show,this.DelayTime);
//setInterval('test.show()',this.DelayTime);
};
</script>
<script type="text/javascript">
var test=new test();
test.play();
</script>
</head>
<body>
</body>
</html>
感谢大家一起探讨
setInterval调用的函数虽然是test实例的方法,但是test.prototype.show=function(){
alert(this.str);
};
中的this==window对象
觉得类静态变量和全局变量不爽,可以在show函数加参数,把this.str传进去
<!-- saved from url=(0011)about:blank -->
<HTML><HEAD>
<META http-equiv=Content-Type content="text/html; charset=unicode">
<META content="MSHTML 6.00.2900.3199" name=GENERATOR></HEAD>
<BODY><SCRIPT LANGUAGE="JavaScript">
<!--
function test(){
this.f1="";
this.DelayTime=1000;
}
test.prototype.show=function(){
this.f1 = "fdsafsda";
alert(this.f1)
};
test.prototype.play = function(){
setInterval(this.show,this.DelayTime);
};var t=new test()
t.play()//-->
</SCRIPT>
</BODY></HTML>
//评点:此时的this要放在window领域内的this,因为此处的this是字符串内的this,并不是本对象this