我在函数的顶部声明了一个变量为this.current_test = null;,然后又有了一个setInterval函数,我需要将该变量设置为一个新参数……
this.current_test.failed = true;
代码:
timer = this.window.setInterval(function()
{
this.window.clearInterval(timer);
this.current_test.failed = true;
},1000);
}但是,我收到一个TypeError: 'undefined' is not an object (evaluating 'this.current_test.failed = true'错误
我假设这是因为setInterval函数中没有定义this.current_test,那么如何编辑该变量呢?
发布于 2012-06-26 11:46:21
定时器函数中的“this”的作用域将不会引用this.window。此范围仅适用于您可以执行的for函数
var wnd=this.window; // take your widow to local variable
timer = this.window.setInterval(function()
{
this.window.clearInterval(timer);
wnd.current_test.failed = true; // use your local variabe in the function
},1000);
}顺便说一下,对于window,为什么需要'this‘,而且如果cuurent_test是一个全局变量,那么可以像这样声明
var current_test;您可以在定时器函数中使用全局变量
https://stackoverflow.com/questions/11200477
复制相似问题