setInterval参数与Javascript类的问题

wangbin20001216 2007-04-21 04:30:27
我是一个菜鸟,我做了一段代码,但老是出问题.
这个CASE是这样个的.就是我一点"开始"线的宽度就开始增加,一点"停止"就停止.
附代码如下:请高手帮忙.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Write The Codes By Myself</title>
<script language="javascript">
<!--hidden
function cl()
{
this.timer=null;
}
cl.prototype.st=function(n){this.timer=window.setInterval("this.add('"+n+"')",100);}
cl.prototype.add=function(n)
{
var obj=document.getElementById(n);
obj.style.width=parseInt(obj.style.width)+10;
}
cl.prototype.stoped=function() { window.clearInterval(this.timer); }
var my=new cl();

-->
</script>

</head>

<body>
<hr style="width:10px" id="line">
<form>
<input type="button" name="Submit" value="开始" onClick="javascript:my.st('line');">
<input type="button" name="Submit2" value="停止" onClick="javascript:my.stoped();">
</form>
</body>
</html>
...全文
1316 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangbin20001216 2007-04-24
  • 打赏
  • 举报
回复
谢谢楼上的各位朋友帮忙,同时感谢adverse(King)还给我介绍了一些原理的知识.谢谢.向你们学习.我采用了zhaoxiaoyang(梅雪香@深圳) 的方法,因为跟我最近学的比较一致.再次感谢.

dh20156(风之石) 给我推荐的setTimeout是我学习的下一个命令.我会串通学习的.

我是新手,给分怎么给呀?
dh20156 2007-04-21
  • 打赏
  • 举报
回复
也许用setTimeout更好:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Write The Codes By Myself</title>
<script language="javascript">
<!--hidden
function cl(){
this.timer = null;
}
cl.prototype.add = function(n){
var obj = document.getElementById(n);
obj.style.width = parseInt(obj.style.width)+10;
cl.prototype.timer = window.setTimeout(function(){cl.prototype.add(n)},1000);
}
cl.prototype.stoped = function(){
if(cl.prototype.timer!=null){
window.clearTimeout(cl.prototype.timer);
}
cl.prototype.timer = null;
}

var my = new cl();

-->
</script>

</head>

<body>
<hr style="width:10px" id="line">
<form>
<input type="button" name="Submit" value="开始" onClick="javascript:my.add('line');">
<input type="button" name="Submit2" value="停止" onClick="javascript:my.stoped();">
</form>
</body>
</html>
adverse 2007-04-21
  • 打赏
  • 举报
回复
cl.prototype.st=function(n){this.timer=window.setInterval("this.add('"+n+"')",100);}
这句话有问题,在window执行this.add的时候,这个this并不是你所想的my对象,而是window(window.setInterval这个方法是属于window的,这个方法里用this当然也是指向window的)。只要改成对象引用就好了,my.add。
adverse 2007-04-21
  • 打赏
  • 举报
回复
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Write The Codes By Myself</title>
<script language="javascript">
<!--hidden
function cl()
{
this.timer=null;
}
cl.prototype.st=function(n){this.timer=window.setInterval("my.add('"+n+"')",100);}
cl.prototype.add=function(n)
{
var obj=document.getElementById(n);
obj.style.width=parseInt(obj.style.width)+10;
}
cl.prototype.stoped=function() { window.clearInterval(this.timer); }
var my=new cl();

-->
</script>

</head>

<body>
<hr style="width:10px" id="line">
<form>
<input type="button" name="Submit" value="开始" onClick="javascript:my.st('line');">
<input type="button" name="Submit2" value="停止" onClick="javascript:my.stoped();">
</form>
</body>
</html>
梅雪香 2007-04-21
  • 打赏
  • 举报
回复
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Write The Codes By Myself</title>
<script language="javascript">
<!--hidden
function cl()
{
this.timer=null;
}
cl.prototype.st=function(n){
var cc = this;
var c = function (){ cc.add(n) ; };
this.timer=window.setInterval(c,100);
}
cl.prototype.add=function(n)
{
var obj=document.getElementById(n);
obj.style.width=parseInt(obj.style.width)+10;
}
cl.prototype.stoped=function() {
window.clearInterval(this.timer);
}
var my=new cl();

-->
</script>

</head>

<body>
<hr style="width:10px" id="line">
<form>
<input type="button" name="Submit" value="开始" onClick="javascript:my.st('line');">
<input type="button" name="Submit2" value="停止" onClick="javascript:my.stoped();">
</form>
</body>
</html>

87,910

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧