typescript 调用方法表单 setInterval() 导致异常

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24477246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:16:27  来源:igfitidea点击:

Call method form setInterval() causing exception

typescriptthis

提问by Jviaches

I would like to call function from setInterval(). Here is the idea:

我想从 setInterval() 调用函数。这是一个想法:

class Greeter {
    element: HTMLElement;
    span: HTMLElement;
    timerToken: number;

    constructor(element: HTMLElement) {
        this.element = element;
        this.element.innerHTML += "The time is: ";
        this.span = document.createElement('span');
        this.element.appendChild(this.span);
        this.span.innerText = new Date().toUTCString();
        //this.element.style.cssText = "-webkit-transform:rotate(7deg)";     
        //this.element.style.transition = "-webkit-transform: rotate(180deg)";         
    }

    start() {
        this.timerToken = setInterval(this.runningLoop(this.element), 500);        
    }

    stop() {
        clearTimeout(this.timerToken);
    }

    runningLoop(element: HTMLElement) {
        this.element.style.cssText = "-webkit-transform:rotate(7deg)";         
    }


}

window.onload = () => {
    var el = document.getElementById('content');
    var greeter = new Greeter(el);

    greeter.start();
};        

In this case i getting an exception:

在这种情况下,我得到一个例外:

Unhandled exception at line 13, column 9. Microsoft JScript runtime error: Invalid argument.

第 13 行第 9 列未处理的异常。Microsoft JScript 运行时错误:参数无效。

So i tried as following :

所以我尝试如下:

this.timerToken = setInterval(function () { this.runningLoop(this.element) }.bind, 500);

No exception but nothing happens..

没有例外,但没有任何反应..

Any ideas ?

有任何想法吗 ?

回答by Anthony Chu

setInterval(this.runningLoop(this.element), 500);

The above invokes this.runningLoopbefore passing it to setInterval, setIntervalis expecting a function but is receiving undefined. Wrap the call in an arrow function...

以上this.runningLoop在将其传递给 之前调用setIntervalsetInterval正在等待一个函数但正在接收undefined。将调用包装在箭头函数中...

setInterval(() => this.runningLoop(this.element), 500);

And since you're not using the element argument in runningLoop, you can remove the argument and pass the method to setInterval...

并且由于您没有在 中使用 element 参数runningLoop,因此您可以删除该参数并将方法传递给setInterval...

setInterval(this.runningLoop, 500);

回答by Carlos Galeano

This will be looping in the time that you want: setInterval(() => function(), time in miliseconds);

这将在您想要的时间内循环: setInterval(() => function(), time in miliseconds);

Now, if you want to stop whenever! Write a var before the interval: Declare on the top of your TS file: varInterval: any; this.varInterval = setInterval(() => function(), time in miliseconds);

现在,如果你想随时停下来!在间隔前写一个var:在你的TS文件顶部声明:varInterval: any; this.varInterval = setInterval(() => function(), 时间以毫秒为单位);

You can call the method to stop like this: clearInterval(this.varInterval);

您可以像这样调用停止方法: clearInterval(this.varInterval);

This work for me!

这对我有用!