简体   繁体   English

为什么为同一函数调用window.setTimeout不会引入无限循环?

[英]Why calling of window.setTimeout for the same function doesn't introduce infinite loop?

I saw the following code: 我看到了以下代码:

class MasterControlPanel {
    private sensors: Sensor[] = [];
    constructor() {
        // Instantiating the delegate HeatSensor
        this.sensors.push(new HeatSensor(this));
    }

    start() {
        for (var i= 0; i < this.sensors.length; i++) {
            // Calling the delegate
            this.sensors[i].check();
        }
        window.setTimeout(() => this.start(), 1000);
    }

    startAlarm(message: string) {
        console.log('Alarm! ' + message);
    }
}

var cp = new MasterControlPanel();
cp.start();

Why window.setTimeout(() => this.start(), 1000); 为什么window.setTimeout(() => this.start(), 1000); doesn't introduce infinite loop? 不引入无限循环?

Based on my understanding, cp.start() will first iterate each sensor inside sensors and then call the window.setTimeout which in turn calls the start again after 1 second delay. 根据我的理解, cp.start()将首先迭代sensor内部的每个sensors ,然后调用window.setTimeout ,该方法又会在延迟1秒后再次调用start

Reference: Listing 3-3. 参考:清单3-3。 Delegation in Pro TypeScript: Application-Scale JavaScript Development Pro TypeScript中的委托:应用程序级JavaScript开发

In short, it doesn't introduce an infinite loop because setTimeout doesn't block. 简而言之,它不会引入无限循环,因为setTimeout不会阻塞。 Instead, the function you give it to execute, () => this.start() is added to a queue and executed after 1 second of time passes. 而是将您赋予它的函数执行()=> this.start()添加到队列中,并在经过1秒后执行。 The start method returns after calling window.setTimeout, and thus doesn't cause an infinite loop in the imperative sense. start方法在调用window.setTimeout之后返回,因此从命令意义上讲不会导致无限循环。 Most of the time, the code will be in a state of waiting until the next timer comes up. 在大多数情况下,代码将处于等待状态,直到下一个计时器出现。

That the code infinitely schedules a timer to call the start method is also true, but it isn't an infinite loop because it returns control to the javascript runtime. 代码无限地调度计时器以调用start方法也是正确的,但它不是无限循环,因为它将控制权返回给javascript运行时。

It does introduce an infinite loop, delayed 1 sec between each call 确实引入了无限循环,每个调用之间延迟了1秒

window.setTimeout(() => this.start(), 1000);

is almost the same as 几乎与

var _this = this; 
window.setTimeout(function(){ _this.start(); }, 1000);

it's called Arrow Function 它被称为箭头功能

Maybe you need setInterval . 也许您需要setInterval That one would create a infinite loop which you can clear by using clearInterval . 那将创建一个无限循环,您可以使用clearInterval清除该循环。

this.interval = window.setInterval(() => {
...
}, 5000);

window.clearInterval(this.interval);

Also you can do 你也可以做

this.sensors.forEach(sensor => sensor.check())

Instead of using for loop. 而不是使用for循环。

You can't use this in the setTimeout, because it refers to the setTimeout, not to your class. 您不能在setTimeout中使用它,因为它引用的是setTimeout,而不是您的类。 Try this : 尝试这个 :

class MasterControlPanel {
    private sensors: Sensor[] = [];
    constructor() {
        // Instantiating the delegate HeatSensor
        this.sensors.push(new HeatSensor(this));
    }
    var that = this;
    start() {
        for (var i= 0; i < this.sensors.length; i++) {
            // Calling the delegate
            this.sensors[i].check();
        }
        window.setTimeout(() => that.start(), 1000);
    }

    startAlarm(message: string) {
        console.log('Alarm! ' + message);
    }
}

var cp = new MasterControlPanel();
cp.start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM