在对象内使用setInterval [英] Using setInterval inside an object

查看:87
本文介绍了在对象内使用setInterval的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello =)


我有一个对象,其中包含一个应该每x ms执行一次的方法。我可以在对象构造中使用setInterval,就像这样 -


self.setInterval(''ObjectName.methodName()'',this.pinginterval);


- 但如果不使用文字ObjectName,是否有办法做到这一点?如果我

写''this.methodName()''我得到'第1行Char 1:对象不支持这个

属性或方法。在IE中,Firebird没有任何反应。


谢谢,

Daniel

Hello =)

I have an object which contains a method that should execute every x ms. I
can use setInterval inside the object construct like this -

self.setInterval(''ObjectName.methodName()'', this.pinginterval);

- but is there no way to do this without using the literal ObjectName? If I
write ''this.methodName()'' I get "Line 1 Char 1: Object doesn''t support this
property or method." in IE, and nothing happens in Firebird.

Thank you,
Daniel

推荐答案

" Daniel" <所以************ @ i-get-virus-and-spam.com>写道:
"Daniel" <so************@i-get-virus-and-spam.com> writes:
我有一个对象,其中包含一个应该每x ms执行一次的方法。我可以在这样的对象构造中使用setInterval -

self.setInterval(''ObjectName.methodName()'',this.pinginterval);

- 但没有使用文字ObjectName没有办法做到这一点?如果我写''this.methodName()''我得到'第1行Char 1:对象不支持这个
属性或方法。在IE中,Firebird没有任何反应。
I have an object which contains a method that should execute every x ms. I
can use setInterval inside the object construct like this -

self.setInterval(''ObjectName.methodName()'', this.pinginterval);

- but is there no way to do this without using the literal ObjectName? If I
write ''this.methodName()'' I get "Line 1 Char 1: Object doesn''t support this
property or method." in IE, and nothing happens in Firebird.




因为这个运行时的关键字没有引用

对象。这是因为传递代码而感到厌倦的原因之一是

字符串。将它作为函数值传递保留文本范围,

因此函数中的标识符指的是函数写入时所存在的那些,而不是它所在的位置执行。


我会写


var我=这个;

函数callMethod(){

myself.methodName();

}

setInterval(callMethod,this.pinginterval)


那样当执行代码

时,你不依赖任何文字是相同的,但是依靠正常的范围规则来保持

的*值*函数的范围链。


/ L

-

Lasse Reichstein Nielsen - lr*@hotpop.com

Art D''HTML:< URL:http://www.infimum.dk/HTML/randomArtSplit .html>

''没有判断的信仰只会降低精神神圣。''



Because the "this" keyword at the time of running doesn''t refer to the
object. That is one reason to be weary about passing around code as
strings. Passing it around as a function value keeps the textual scope,
so the identifiers in the function refer to the ones that existed where
the function was written, not where it is executed.

I would write

var myself = this;
function callMethod() {
myself.methodName();
}
setInterval(callMethod, this.pinginterval)

That way you don''t rely on any literals being the same when the code
is executed, but rely on the normal scope rules to keep the *value* of
myself in the function''s scope chain.

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D''HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
''Faith without judgement merely degrades the spirit divine.''


>您需要以某种方式将对象存储在一个变量中,该变量是
> You would need to store the object somehow in a variable that is
,可以从传递给setInterval的代码中访问
例如,当您创建对象时,将它们存储在一个数组
函数AClass中( ){
this.id = AClass.instances.length;
AClass.instances [this.id] = this;
}
AClass.instances = new Array() setInterval('''AClass.instances [''+ this.id +''] .methodName();'',延迟)
accessible from the code passed to setInterval
For example when you create objects store them in an Array
function AClass () {
this.id = AClass.instances.length;
AClass.instances[this.id] = this;
}
AClass.instances = new Array()
Then in your method you script
setInterval(''AClass.instances['' + this.id + ''].methodName();'', delay)




我想到了这个。实际上,我认为你有责任让我这么想,因为我觉得你是那个给我一个类似方法的人,我问b
询问如何确定一个对象的运行时指定名称没有

必须自己存储=)

我会选择Lasse的解决方案(不进攻;),因为那个

不需要手动跟踪对象名称。


但是谢谢你的帖子=)


Daniel



I thought of this. Actually, I think you''re responsible for me thinking
this, as I think you were the one who gave me a similar approach when I
asked about how to determine an object''s run-time assigned name without
having to store it oneself =)
I''m gonna go with Lasse''s solution, though (no offense ;), since that
requires no manual tracking of object names.

But thank you for your post =)

Daniel


" Daniel" <所以************ @ i-get-virus-and-spam.com>写道:
"Daniel" <so************@i-get-virus-and-spam.com> writes:
PS:Er vi egentlig ikke landsm?nd?
PS: Er vi egentlig ikke landsm?nd?




Jo,det er vi da :) />
(如果您在丹麦语中询问了
组< URL:news:dk.edb.internet.webdesign.clientside> :),我会用丹麦语回答。)


/ L

-

Lasse Reichstein Nielsen - lr*@hotpop.com

Art D''HTML:< URL:http://www.infimum.dk/HTML/randomArtSplit.html>

''没有判断的信仰只会降低精神神圣。''



Jo, det er vi da :)
(and I would have answered in Danish if you had asked in the Danish
group <URL:news:dk.edb.internet.webdesign.clientside> :) )

/L
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D''HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
''Faith without judgement merely degrades the spirit divine.''


这篇关于在对象内使用setInterval的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆