2012-06-26 2 views
0

내가 변수가 this.current_test = null;로 내 함수의 맨 위에 선언 한 후 선 아래로 나는 setInterval을 기능을 가지고 있고이 새 매개 변수에 변수를 설정해야합니다 ...하여 setInterval 변수 범위

this.current_test.failed = true;

코드 :

나는 TypeError: 'undefined' is not an object (evaluating 'this.current_test.failed = true' 오류

을 얻고 그러나

timer = this.window.setInterval(function() 
    { 
     this.window.clearInterval(timer); 
     this.current_test.failed = true; 
    },1000); 
} 

그리고 나는이 일 가정 왜냐하면 this.current_test가 setInterval 함수 내에 정의되어 있지 않기 때문에 어떻게 그 변수를 편집 할 수 있습니까?

+0

코드를 더 게시 할 수 있습니까? 특히'current_test'를 정의하는 부분. –

답변

0

타이머 기능에서 'this'의 범위는 this.window를 참조하지 않습니다. 이 범위는 '이'또한 cuurent_test 경우 전역 변수가 다음

var current_test; 
같이 선언 할 수있다 필요 왜 함수 만이 창에 참고로하는 것에 대한 그런데

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); 
} 

을 할 수있는 것입니다

타이머 기능 내에서 전역 변수를 사용할 수 있습니다.