20毫秒以后MyObject对象已经不存在了…………
this指向谁呢?在setTimeout和setInterval里面都不可以用这个
this指向谁呢?在setTimeout和setInterval里面都不可以用这个
解决方案 »
- 急,求救一个页面调用另个页面里的按钮问题
- js读取XML问题
- 让网页最小化按钮变灰
- 在不出现滚动条的情况下,利用鼠标滚轮滚动页面,如何实现?
- 请问怎样利用radio显隐象文本框那样的控件
- 什么是事件的冒泡和捕获?
- ().delegate 委托事件对鼠标上移动效果控制 一些问题
- 请教诸位大侠一个有关xml的问题,老美限我二十四小时内解决,请大伙多多帮忙提供一个思路啦
- 在页面meta中用refresh,能不能在refresh时不运行<body>的onload事件?
- 写了一个倒计时效果练手,请教如何改善?谢谢!
- 如何取得文件下所有文件的名字?
- ---------》如何点击图片按钮后不提交表单??《------------
obj.a = ...
obj.fn = ...
obj.OnTimer = ...setTimeout( "obj.OnTimer()", 20 );
// setTimeout()
MyObject obj( .. );
{
this.a = ...
this.fn = ...
this.OnTimer = ...
// ...
setTimeout( "eval(this.OnTimer())", 20 );
}
系统肯定不会认为this是你创建的对象,什么是this?
setTimeout不是我们定义的对象的方法或属性,所以。。
出现这种情况是正常的。
setTimeout("method()", 10) 等效于 setTimeout("window.method()", 10)
所以你在它里用this就变味了 setTimeout( "window.this.OnTimer()", 20 );
这里明显就不对了, 所以我在写JS类的时候都这样处理一下:
function MzPopupMenu(Tname)
{
if(typeof(Tname) != "string" || Tname == "")
throw(new Error(-1, 'Please input MzMenu instance name')); setTimeout( Tname +".OnTimer()", 20 );
//....
}
var obj = new MyObject("obj");
this.value=1;
this.n=1;
this.foo=foo;
}
function foo(){
this.n+=this.value;
window.setTimeout("this.foo()",100);
}
(new Test()).foo();
这样可以。
function foo()
{
this.n+=this.value;
alert(this.n);
window.setTimeout("this.foo()",1000);
}
比如说我 alert(this.n), 由于第一次调用的是 new Test().foo(), 弹出的值是对, 但是在后面弹出的值就不对了. 因为你以后调用的 foo() 已经不是new Test()这个类里的方法而是直接调用 window.foo() 这个函数了.
function Test(){
this.value=1;
this.n=1;
this.foo=foo;
}
function foo(){
var self = this;
this.n+=this.value;
alert(this.n);
f = function(){ self.foo() }
window.setTimeout(f,1000);
}
(new Test()).foo()
</script>
<script>
function foo(){
this.n+=this.value;
//alert(this.n);
}
function Test(){
this.value=1;
this.n=1;
this.foo=foo;
f = function(){ this.foo() }
window.setInterval(f,1000);}(new Test()).foo()
</script>
<script>
function foo(){
this.n+=this.value;
alert(this.n);
}
function Test(){
this.value=1;
this.n=1;
this.foo=foo;
var me = this;
f = function(){me.foo() }
window.setInterval(f,1000);}(new Test()).foo()
</script>
<head>
<title>
Hello Timer
</title>
<script language = "JScript">function Obj()
{
function foo()
{
alert( "Hello" );
}
this.timer = foo;
var me = this;
var f = function()
// 必须这样,
{ me.timer(); };
// 不能这样
// { this.timer(); };
setInterval( f, 1000 );
}var o = null;function OnClick()
{
o = new Obj();
// ok
//setInterval( "o.timer()", 1000 );
}</script>
</head>
<body>
<input type = "button" onclick = "OnClick()" value = "Click me"></input>
</body>
</html>但不知道为什么要使用一个中间对象。而不能直接使用this.
{
function setTimer( obj )
{
setTimeout( function() { obj.BuildA(); }, obj.timer );
} function foo()
{
alert( "Hello" );
}
this.timer = foo;
/*
var me = this;
var f = function()
// 必须这样,
{ me.timer(); };
// 不能这样
// { this.timer(); };
setInterval( f, 1000 );
*/
setTimer( this );
}var o = null;function OnClick()
{
o = new Obj();
// ok
//setInterval( "o.timer()", 1000 );
}
<input type='text' name='ice' value='iceberg' onclick='alert(value)'/>像这样的写法this应该是默认为事件的srcElement吧
这里的value = this.value
var me = this;
f = function(){me.foo() }
window.setTimeout(f,1000);
实际上就是将具体某个对象实例的地址引用传过去了