// constructor function MyObj { function foo() { alert( this.data ); }
this.timer = foo; this.data = "Hello";
setInterval( "this.timer()", 1000 ); }
function Another() { // create timer when create object var obj = new MyObj();
}
However, it doesn't work as you think. The reason is that the setInterval() function does not recognize the variable this. A workaround approach could be like this.
function Another() { var obj = nw MyObj(); setInterval( "obj.timer()", 1000 ); }
Obviously, it can work correctly, but if you are a perfectionist Otherwise, you won't be satisfied with it. Fortunately, you can put this action into the constructor, with a slight change in form.
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn