javascript如何在类中使用定时器?

catxl313 2009-06-19 02:07:36
下面这段代码是不能运行的,但大家应该能很容易的猜出我想干什么,请问我如何才能做到呢?
关于javascript对象模型我一直都不是太明白,不知道这样的功能在javascript中应该如何描述,望高手不吝指教!
如果这个问题确实涉及到很高深的理论,我愿意加分。

function foo( c )
{
this.cnt = c;
this.start = function()
{
setInterval( DoSomething, 1000 );
}
this.DoSomething = function()
{
this.cnt++; // just do something
}
}

var f1 = new foo( 1 );
f1.start();

var f2 = new foo( 100 );
f2.start();

var f3 = new foo( 243 );
f3.start();

...全文
93 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
catxl313 2009-06-22
  • 打赏
  • 举报
回复
我本来想加分的,可是系统提示说:“该帖已超过分数最大值,不允许加分操作。”?!!

唉!

prototyper 的解释一语点中了啊,多谢!
catxl313 2009-06-22
  • 打赏
  • 举报
回复
OK , 多谢各位!
foolbirdflyfirst 2009-06-22
  • 打赏
  • 举报
回复
ls的应该可以阿,就是this的作用域问题。
这样呢,直接用id.
<html>
<head><title>Timer In Class</title>
<script language="javascript">
function foo( id, val )
{
// this.obj = id;
window.document.getElementById(id).value = val;
this.start = function(id)
{
return function(){
setInterval(this.DoSomething, 1000 );//....
}
}(id);
this.DoSomething = function()
{
window.document.getElementById(id).value ++ ; // just do something
}
}

function OnBodyLoaded()
{
var f1 = new foo( "Text1", 1 );
f1.start();

var f2 = new foo( "Text2", 100 );
f2.start();

var f3 = new foo( "Text3", 243 );
f3.start();

}
</script>
</head>
<body onload="OnBodyLoaded();">
<input id="Text1" />
<input id="Text2" />
<input id="Text3" />
</body>
</html>

prototyper 2009-06-22
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 catxl313 的回复:]
楼上的代码在IE6测试失败.....
不过让我见识了self.DoSomething();这样的语法,多谢!
[/Quote]

#7是正确的。“var self = this”是为了保持上下文正确,因为setInterval是window的属性,该函数中的this指向window而不是你的foo。再就是“function(){self.DoSomething()}”方式可以很方便的传递参数或在其中直接编写要运行的代码。
顺便问一下,这样回帖能得到积分吗?我这ID已经无法下载资源了......
catxl313 2009-06-22
  • 打赏
  • 举报
回复
楼上的代码在IE6测试失败.....
不过让我见识了self.DoSomething();这样的语法,多谢!
2009-06-22
  • 打赏
  • 举报
回复

<html>
<head><title>Timer In Class</title>
<script language="javascript">
function foo( id, val )
{
this.obj = id;
var self = this;
window.document.getElementById(this.obj).value = val;
this.start = function()
{
setInterval(function(){self.DoSomething();}, 1000 );//....
}
this.DoSomething = function()
{
window.document.getElementById(this.obj).value ++ ; // just do something
}
}

function OnBodyLoaded()
{
var f1 = new foo( "Text1", 1 );
f1.start();

var f2 = new foo( "Text2", 100 );
f2.start();

var f3 = new foo( "Text3", 243 );
f3.start();
}
</script>
</head>
<body onLoad="OnBodyLoaded();">
<input id="Text1" />
<input id="Text2" />
<input id="Text3" />
</body>
</html>
catxl313 2009-06-22
  • 打赏
  • 举报
回复
不好意思,这个还真能运行?!!
可是为什么传入参数不能是个页面元素的id呢?
比如我写成这样:

function foo( id, val )
{
this.obj = id;
window.document.getElementById(this.obj).value = val;
this.start = function()
{
setInterval(this.DoSomething, 1000 );//....
}
this.DoSomething = function()
{
window.document.getElementById(this.obj).value ++ ; // just do something
}
}

var f1 = new foo( "Text1", 1 );
f1.start();

var f2 = new foo( "Text2", 100 );
f2.start();

var f3 = new foo( "Text3", 243 );
f3.start();



this.DoSomething里面的这一句: window.document.getElementById(this.obj) 总是返回null;
而这一句却能成功:window.document.getElementById(this.obj).value = val;

这是为什么?
感觉有点莫名其妙了!

调试时用这个吧,多谢!
<html>
<head><title>Timer In Class</title>
<script language="javascript">
function foo( id, val )
{
this.obj = id;
window.document.getElementById(this.obj).value = val;
this.start = function()
{
setInterval(this.DoSomething, 1000 );//....
}
this.DoSomething = function()
{
window.document.getElementById(this.obj).value ++ ; // just do something
}
}

function OnBodyLoaded()
{
var f1 = new foo( "Text1", 1 );
f1.start();

var f2 = new foo( "Text2", 100 );
f2.start();

var f3 = new foo( "Text3", 243 );
f3.start();
}
</script>
</head>
<body onload="OnBodyLoaded();">
<input id="Text1" />
<input id="Text2" />
<input id="Text3" />
</body>
</html>
xiangtanboy 2009-06-19
  • 打赏
  • 举报
回复
function foo( c )
{
this.cnt = c;
var me = this;
this.start = function()
{
setInterval( me.DoSomething, 2000 );
}
this.DoSomething = function()
{
me.cnt++; // just do something
//alert(me.cnt);
}
}

var f1 = new foo( 1 );
f1.start();
zhangrun_gz 2009-06-19
  • 打赏
  • 举报
回复
不行就写类外面吧
catxl313 2009-06-19
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 gsh945 的回复:]
好像不能这么玩啊
[/Quote]

那该怎么玩呢?
应该通过什么形式来描述我的设计呢?
郭大侠_ 2009-06-19
  • 打赏
  • 举报
回复
好像不能这么玩啊
foolbirdflyfirst 2009-06-19
  • 打赏
  • 举报
回复
javascript就闭包拉,作用域啦那些东西要特别小心.


function foo( c )
{
this.cnt = c;
this.start = function()
{
setInterval(this.DoSomething, 1000 );//....
}
this.DoSomething = function()
{
this.cnt++; // just do something
}
}

var f1 = new foo( 1 );
f1.start();

var f2 = new foo( 100 );
f2.start();

var f3 = new foo( 243 );
f3.start();

87,921

社区成员

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

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