We call settimeout in a scope or within a class of our own definition, and setinterval often encounter errors that cannot be found for a variable. For example, the following:
window.onload = function () {manager.init ();}; var manager = function () {Slef = this; this . Name = "Manager" ; SetInterval ( this . Timermonitor, 1000 function () {Console . log ( this .name); }}
Let's look at the results under Chrome: We didn't print the manager as expected. Because this.name this variable is undefined state. So how do we solve this problem? Just put SetInterval (Timermonitor, 1000) and change to setinterval (function () {Self.timermonitor ()}, 1000); The complete code is as follows:
function () { manager.init ();}; var manager = { function() { self =this; this. Name = "Manager"; SetInterval (function() {self.timermonitor ()}, ()} , function () { Console.log (this. Name); }}
Let's look at the results of the operation under Chrome:
This time the this.name variable is printed correctly. The problem is solved, the problem is coming, why is it so good? Update the blog later to explain why.
JavaScript does not find a variable when using settimeout, SetInterval (1)